如果没有选择复选框,警惕! .. ELSE ..递交。 (JavaScript)的(<

2019-08-02 17:48发布

语言: 使用Javascript

我真的很讨厌问这样一个看似简单的问题,但简单,因为它似乎,我不能得到这个工作。

我想单独纯JavaScript(无库支持)完全做到这一点。

我有一个复选框形式...
所有的复选框被命名files[]因为我使用一个数组的结果:

<input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
<input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
<input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>

我正在试图做的是,当用户提交表单:

  • 如果没有复选框被选中>>返回警惕!
  • ELSE提交表单

这是我的表格

<form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();">

    <input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
    <input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
    <input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
    <input type="submit" value="Submit" name="submit">

</form>​

这是我的Javascript代码

function confirm_update() {
    var aCheckbox = document.deleteFiles.getElementsByTagName('input');

    if (aCheckbox.checked){
    return confirm("Are you sure you want to proceed deleting the selected files?");

    } else {
        alert("You do not have any selected files to delete.");
        return false;
    }
}​

在行动: http://jsfiddle.net/DVqwB/3/

显然,它不工作,我知道我应该使用getElementsById但因为他们每个人都有唯一的ID,我不能使用。 而且我也知道有很多关于这个网站的解决方案,但如果你看看 - 他们实际使用jQuery ...

任何帮助和指导,将不胜感激! 非常感谢。

Answer 1:

遍历集合正确的方法是:(完整的例子)

 function confirm_update() { var arrCheckboxes = document.deleteFiles.elements["files[]"]; var checkCount = 0; for (var i = 0; i < arrCheckboxes.length; i++) { checkCount += (arrCheckboxes[i].checked) ? 1 : 0; } if (checkCount > 0){ return confirm("Are you sure you want to proceed deleting the selected files?"); } else { alert("You do not have any selected files to delete."); return false; } } 
 body { margin: 30px; } 
 <form name="deleteFiles" action="" method="post" onsubmit="return confirm_update();"> <input type='checkbox' name='files[]' id='1' value='1' /> file 1<br /> <input type='checkbox' name='files[]' id='2' value='2' /> file 2<br /> <input type='checkbox' name='files[]' id='3' value='3' /> file 3<br /> <input type="submit" value="Submit" name="submit" /> </form> 



Answer 2:

getElementsByTagName返回的NodeList(类似的阵列),而不是一个单一的元件。

你必须遍历它并依次测试每个元素,只有当你到达循环的末端仍然没有发现该条件匹配处理“其他”的情景。



Answer 3:

你可以使用这个script.just昌你的文件名形式的行动

    <form id="deleteFiles" name="deleteFiles" action="yourFileName.html" method="post" onsubmit="return confirm_update();">

    <input type='checkbox' name='files[]' id='1' value='1' /> file 1<br>
    <input type='checkbox' name='files[]' id='2' value='2' /> file 2<br>
    <input type='checkbox' name='files[]' id='3' value='3' /> file 3<br>
    <input type="submit" value="Submit" name="submit">

</form>
    <script type="text/javascript">
    function confirm_update() {
        check=false;
        var aCheckbox = document.getElementById('deleteFiles').elements;
        for(i=0;i<aCheckbox.length;++i)
        {
            if(aCheckbox[i].type=='checkbox' && aCheckbox[i].checked)
           {
               check=true;
           }
         }
         if(check==true)
         {
             return confirm("Do u really want to delete?");
        }
        else
            {alert("you haven't selected any of the checkboxex");
            return false;
            }

    }
    </script>


文章来源:
If no checkbox is selected, alert! .. ELSE .. submit. (Javascript)