如果未选中任何复选框,则显示警报()

时间:2012-12-12 10:20:22

标签: javascript checkbox

单击“删除”按钮后,如果未选中任何复选框,如何显示警报。 我的HTML表单          

<form name="Delete" method="post" action="DeleteAdministrator">
                <table width="800px" cellpadding="5" style="color:black; border-top:1px solid #ccc;" border="0">
                </table>
                    <div id="container">
                    <div id="demo_jui">
                    <table id="adminList" class="display" cellpadding="0" cellspacing="0" border="0" style="font-size:12px;" >
                    <thead id="headed">
                    <tr>
                        <th  align="center" title="Select">Select</th>
                        <th align="center" title="Email Address">Email Address</th> 
                        </tr>
                    </thead>
                    <tbody></tbody>
                    </table>
                    </div>
                    </div>
                    <table id="footed">
                        <tr>
                            <td colspan="2">
                                <a href="#"><img src="images/Delete.png" style="border:none;" onClick="javascript:Go()"
                                onmouseover="this.src='images/Delete_Hover.png'" onmouseout="this.src='images/Delete.png'"/></a>
                                &nbsp;&nbsp;&nbsp;
                                <a href="#"><img src="images/Reset.png" style="border:none;" onClick="javascript:clearForm()"
                                onmouseover="this.src='images/Reset_Hover.png'" onmouseout="this.src='images/Reset.png'"/></a>
                            </td>
                        </tr>
                    </table>
                </form>
</body>
</html>

将在表格中检索复选框值。当我测试它时, 当复选框被选中或未选中时,它仍会提示“请选择删除管理员”

在功能Go()中,验证document.Delete.delAdlist[i].checked的方式是否正确?谢谢

<script language="JavaScript">                      
    var running = 0;
    function Go() {
        var f = document.forms[0];
        for (i = 0; i < document.Delete.delAdlist.length) {
            if (!document.Delete.delAdlist[i].checked) {
                alert("Please select to delete admin.");
                return false;
            }
            else {
                f.submit();
                return true;
            }
        }  
        f.submit();
        running++;
    }                               
</script>

3 个答案:

答案 0 :(得分:1)

查找已检查的项目更合乎逻辑,当找到第一个项目时:提交表单并退出。如果未找到任何选项,则会显示警报。您没有显示复选框HTML,因此我假设您有一个名为delAdlist[]的复选框数组。

简单的Javascript解决方案

function Go() {
    var f = document.forms["Delete"];

    for (i = 0; i < f.elements["delAdlist[]"].length; i++) {
       if (f.elements["delAdlist[]"][i].checked) {
            f.submit();
            return true;
        }
    }  

    alert("Please select at least one to delete");
}

jQuery解决方案

function Go() {
    if($('form[name=Delete] input[name=delAdlist\\[\\]]:checked').length > 0)
    {
        $('form[name=Delete]').submit();
    }
    else
    {
        alert("Please select at least one to delete");
    }
}

注意:如果您的复选框是数组,则上述代码将起作用。如果您的复选框不是数组并且都名为delAdlist(不带括号),请从第一个解决方案中删除[],然后将第二个解决方案中的选择器更改为form[name=Delete] input[name=delAdlist]:checked

答案 1 :(得分:0)

您应该检查此格式

var notChecked=0;
for (i = 0; i < document.Delete.delAdlist.length) 
                {
                    if(!document.Delete.delAdlist[i].checked)
                         notChecked=1;

                }  

if(notChecked==1) {
      alert("Please select to delete admin.");
      return false;
}
else {
     f.submit();
}

答案 2 :(得分:0)

<script language="JavaScript">                      
    var running = 0;
    function Go() {
    var f = document.forms[0];
   if( $("input[type='checkbox']:checked").length < 1)
        alert("Please select to delete admin.");
   else
     f.submit();
}