简体   繁体   English

如何检查使用jquery检查的多个复选框?

[英]How to check multiple checkboxes checked using jquery?

i am new to the jquery, it is quite interesting, but i am having a little problem, i am populating multiple checkboxes from database using foreach loop like this, 我是jquery的新手,这很有意思,但我有一点问题,我正在使用foreach循环从数据库填充多个复选框,如下所示,

<? foreach($cities as $city) { ?>
    <input type="checkbox" name="city[]" value="<?=$city->id?>" id="city[]" />
<? } ?>

i want to restrict user to check atleast one checkbox, i know how to do this with only one checkbox, but got confused with this kind of array in jquery, any help will be greatly appreciated! 我想限制用户检查至少一个复选框,我知道如何只用一个复选框,但在jquery中与这种数组混淆,任何帮助将不胜感激! Many thanks in advance! 提前谢谢了!

To find how many checkboxes are checked, you can use something like: 要查找检查了多少个复选框,您可以使用以下内容:

var checkedNum = $('input[name="city[]"]:checked').length;
if (!checkedNum) {
    // User didn't check any checkboxes
}

Since you're providing the same name attribute to all the checkboxes (from your PHP loop), you can use the selector input[name="city[]"] to target and find them all. 由于您为所有复选框(从PHP循环)提供相同的name属性,因此您可以使用选择器input[name="city[]"]来定位并查找所有复选框。 But to find out how many specifically are checked , you can add the :checked selector. 但要查明具体检查的数量 ,可以添加:checked选择器。 An alternative to this is using $('input[name="city[]"]').filter(":checked") . 另一种方法是使用$('input[name="city[]"]').filter(":checked")

Finally, !checkedNum will only pass if checkedNum is 0, since 0 is falsey. 最后, !checkedNum 只有checkedNum为0时才会通过,因为0是假的。 Any other number is truthy, and wouldn't satisfy the condition !checkedNum . 任何其他数字都是真实的,并且不满足条件!checkedNum


References: 参考文献:

If you want at least one checkbox checked, you can use this 如果您希望至少选中一个复选框,则可以使用此选项

var somethingChecked = false;
$("input[type=checkbox]").each(function() {
  if(this).is(':checked')) {
    somethingChecked = true;
  }
});
if(!somethingChecked) {
  alert("You haven't checked anything yet");
}

What this does is initialize a variable to false. 这样做是将变量初始化为false。 Then the script loops through all inputs of type checkbox. 然后脚本循环遍历所有类型复选框的输入。 If the item is checked, set the variable to true. 如果选中该项,请将变量设置为true。 Finally, check if the variable is still false. 最后,检查变量是否仍为false。 If it is, then show an error. 如果是,则显示错误。

Assuming you have #my_form as the ID of your form, you could do 假设你有#my_form作为表单的ID,你可以这样做

$("#my_form input[type=checkbox]:checked"). // ... do something

to select and do something with the checked checkboxes. 使用选中的复选框选择并执行某些操作。 You can also do 你也可以

$("#my_form input[type=checkbox]").each(function(idx, elem) {
   var is_checked = $(this).prop("checked");
   // Do something with is_checked
});

to iterate through all the checkboxes and check whether they are checked or not. 迭代所有复选框并检查它们是否被选中。

First of all id of the checkboxes should be unique. 首先,复选框的id应该是唯一的。

Do like this 这样做

<? 
$checkBoxCount = 0;
foreach($cities as $city) { ?>
    <input type="checkbox" name="city[]" value="<?=$city->id?>" id="chkbox-<?=$checkBoxCount?>" />
<? } ?>

Now in jQuery check like this to get all the checkboxes thats checked. 现在在jQuery中检查这样就可以获得所有选中的复选框。

var checkCount = $("input[name='city[]']:checked").length;

if(checkCount == 0)
{
   alert("Atleast one checkbox should be checked.");
}

This code work well for me,here i convert array to string with ~ 这对我的代码工作很好,我在这里转换阵列串

    <input type="checkbox" value="created" name="today_check"><strong>Created</strong>
    <input type="checkbox" value="modified" name="today_check><strong>Modified</strong>
 <a class="get_tody_btn">Submit</a>

<script type="text/javascript">
    $('.get_tody_btn').click(function(){
        var vals = "";
        $.each($("input[name='today_check']:checked"), function(){  
            vals += "~"+$(this).val();  
        });
        if (vals){
            vals = vals.substring(1);
        }else{
            alert('Please choose atleast one value.');
        }


    });
</script>

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

相关问题 jQuery-检查一次是否选中一个复选框 - Jquery - check if checkboxes are checked one at a time 如何使用jQuery获取所有选中复选框的值 - How to get the values of all checked checkboxes using jQuery Javascript:使用JavaScript选中多个复选框 - Javascript: Multiple checkboxes to be checked using javascript 使用codeigniter将多个复选框中的每个“选中”复选框值插入数据库 - Insert each “checked” check box values from multiple checkboxes to the database using codeigniter 如何按名称获取复选框值并检查这些值是否存在于另一个数组中,检查 jquery laravel? - how to get checkboxes values by name and check if these value exists in another array, become checked jquery laravel? 如何标记jquery中的所有复选框 - How to mark checked all checkboxes from jquery 如何使用Ajax,PHP和JQuery选中多个输入复选框? - How to check multiple input checkboxes with Ajax, PHP and JQuery? jQuery UI复选框-仅在选中所有子类别时才检查类别 - Jquery UI Checkboxes - Check category only if all subcategories are checked jQuery-在单击时,选中类中的所有复选框,如果选中,则选中(执行此操作),否则(选中) - jQuery - On click, check all checkboxes in class, if checked (do this) if not (then) 如何在jQuery数据表的多个页面上获取所有选中的复选框 - How do I get all the checked checkboxes on multiple pages of a jQuery datatable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM