【问题标题】:How to show an alert if all checkboxes aren't checked?如果未选中所有复选框,如何显示警报?
【发布时间】:2014-04-23 05:35:39
【问题描述】:

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

如果 ALL 复选框被选中,则发出警报(需要帮助)

复选框:

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

【问题讨论】:

  • id 应该是唯一的...! ://
  • 可能需要使用循环来测试所有人并使您的id 独一无二,以便您可以这样做。
  • 嘿,上面的复选框正在进入 php 循环本身.. 我的意思是值在循环中......
  • @Arun :感谢 +1 的 2 个 jsfiddle ,伙计们怀疑你为什么使用“var $checks”,它应该是“var checks”,因为当我使用 $checks 时它向我显示了错误但是用“ var 检查”它的工作。

标签: javascript php jquery checkbox alert


【解决方案1】:

试试,

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?");
}

【讨论】:

  • @user3209031 很高兴为您提供帮助!
【解决方案2】:
$(\"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.
}

})

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2019-08-24
    • 2014-02-18
    • 2015-08-08
    • 1970-01-01
    相关资源
    最近更新 更多