如果未选中所有复选框,如何显示警报?

时间:2014-04-23 05:35:39

标签: javascript php jquery checkbox alert

如果选中复选框,我想要提醒( - 这是有效的)

如果选中所有复选框,则会发出警报(需要帮助)

CheckBox:

<input type="checkbox" value="1" id="data" name="data[]">
<input type="checkbox" value="2" id="data" name="data[]">
<input type="checkbox" value="3" id="data" name="data[]">

按钮:

<input name=\"submitclose\" type=\"submit\"  value=\"Close\" id=\"submitclose\">

以下是我的Jquery:

echo "<script>
jQuery(function($)  {
$(\"input[id='submitclose']\").click(function() {  

                var count_checked = $(\"[id='data']:checked\").length; 
                if (count_checked == 0)  {
                        alert(\"Please select a Packet(s) to Close.\");
                        return false;
                } else{     
                        return confirm(\"Are you sure you want to Close these Packet?\");
                }  
        });
}); 
</script>";

2 个答案:

答案 0 :(得分:4)

尝试,

HTML:

<input type="checkbox" value="1" id="data1" name="data[]">
<input type="checkbox" value="2" id="data2" name="data[]">
<input type="checkbox" value="3" id="data3" name="data[]">

JS:

var allCheckBox = $("[id^='data']")
var count_checked = allCheckBox.filter(":checked").length; 
if (count_checked == 0)  {
    alert("All check boxes are not checked");
} else if(count_checked != allCheckBox.length) {
    alert("some of the check boxs are not checked");
} else{     
    return confirm("Are you sure you want to Close these Packet?");
}

答案 1 :(得分:0)

$(\"input[id='submitclose']\").click(function(){
    var count = 0;
    $('input#data').each(function(){   
    if ($(this).attr('checked') == true){
    count++  //if a checkbox is checked the variable count will be greater then 0.
    }
})
    if (count == 0){      //nothing is checked.
    alert("Please check at least one of the checkboxes");
}else{       //something is checked.
    //code to execute.
}

})