简体   繁体   English

<FORM> 如果未选中任何复选框,请提醒! .. ELSE ..提交。 (JavaScript)的

[英]<FORM> If no checkbox is selected, alert! .. ELSE .. submit. (Javascript)

Language: Javascript 语言: Javascript

I really hate to ask such a seemingly simple question, but simple as it seems, I can't get this to work. 我真的很讨厌问这样一个看似简单的问题,但看起来很简单,我无法让它发挥作用。

I am trying to do this entirely with pure Javascript alone (no library support) . 我试图完全使用纯Javascript(没有库支持)

I have a form with checkboxes... 我有一个带复选框的表格......
All checkboxes are named files[] because I use the results in an array: 所有复选框都命名为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>

What I'm trying to do is, when the user submits the form: 我想要做的是,当用户提交表单时:

  • IF no checkbox is checked >> return ALERT! 如果没有选中复选框>>返回ALERT!
  • ELSE submit the form ELSE提交表格

Here's my form : 这是我的表格

<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>​

And here's my Javascript code : 这是我的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;
    }
}​

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

Apparently, it is not working, I know I should use getElementsById but since they each have unique IDs I can't use that. 显然,它不起作用,我知道我应该使用getElementsById但由于它们每个都有唯一的ID我不能使用它。 And I also know that there are lots of solutions on this site, but if you look - they actually use jQuery... 而且我也知道这个网站上有很多解决方案,但如果你看 - 他们实际上使用jQuery ...

Any help & guidance would be greatly appreciated! 任何帮助和指导将不胜感激! Thank you so much. 非常感谢。

Correct way to iterate the collection will be: (full example) 迭代集合的正确方法是:(完整示例)

 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> 

getElementsByTagName returns a NodeList (which is like an array), not a single element. getElementsByTagName返回NodeList(类似于数组),而不是单个元素。

You have to loop over it and test each element in turn, and only handle the "else" scenario if you get to the end of the loop without finding a match for the condition. 您必须循环遍历它并依次测试每个元素,并且只有在到达循环结束时才处理“else”方案而不查找条件匹配。

you can use this script.just chang your filename in action of form 你可以使用这个脚本。只需在表格的行动中改变你的文件名

    <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM