[英]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: 参考文献:
:checked
selector: http://api.jquery.com/checked-selector/ :checked
选择器: http : //api.jquery.com/checked-selector/ .length
property: http://api.jquery.com/length/ .length
属性: http : //api.jquery.com/length/ 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.