admin管理员组文章数量:1313731
I have a big form with several checkboxes, having names chkbx0..chkbxN for them (N is known by PHP code which generates the form). I want to create a link-like entity which will invert the checked status (so, what was checked bees unchecked and vice versa) for all the checkboxes when it's clicked. So basically I am interested in a javascript function which has a parameter (the "N") and does the work, and I would call it something like this:
<p>You can reverse checkboxes <a href="javascript:chkbxsnegate(<?php echo $n; ?>);">by clicking here</a>.</p>
The checkboxes are something like these:
<input type="checkbox" name="chkbx0">
<input type="checkbox" name="chkbx1">
...
I use the parameter "N", because there can be more checkboxes, but I only need to invert checked status only till "N".
Please suggest me a javascript code fragment to implement function "chkbxsnegate". Thanks a lot in advance.
I have a big form with several checkboxes, having names chkbx0..chkbxN for them (N is known by PHP code which generates the form). I want to create a link-like entity which will invert the checked status (so, what was checked bees unchecked and vice versa) for all the checkboxes when it's clicked. So basically I am interested in a javascript function which has a parameter (the "N") and does the work, and I would call it something like this:
<p>You can reverse checkboxes <a href="javascript:chkbxsnegate(<?php echo $n; ?>);">by clicking here</a>.</p>
The checkboxes are something like these:
<input type="checkbox" name="chkbx0">
<input type="checkbox" name="chkbx1">
...
I use the parameter "N", because there can be more checkboxes, but I only need to invert checked status only till "N".
Please suggest me a javascript code fragment to implement function "chkbxsnegate". Thanks a lot in advance.
Share Improve this question asked Mar 14, 2012 at 15:48 LGBLGB 7181 gold badge10 silver badges20 bronze badges 05 Answers
Reset to default 4The following will work in most browsers, except IE7 and below (but it works in IE8 and 9)
function chkbxsnegate(n) {
var qsa = document.querySelectorAll("input[type=checkbox][name=chkbx"+n+"]"),
l = qsa.length, i;
for( i=0; i<l; i++) qsa[i].checked = !qsa[i].checked;
}
There is a shorter solution:
$('input[type="checkbox"]').each(function () {
$(this).prop('checked', !$(this).is(':checked'));
});
Even shorter:
$("[type=checkbox]").each(function() {
this.checked = !this.checked;
});
You can do it using Jquery
$('input[name^=chkbx]').each(function() {
if ($(this).is(':checked'))
{
$(this).prop('checked', false);
}
else
{
$(this).prop('checked', true);
}
});
If I well understood you need to reverse the checked
attribute until n^th
element (from 0
to n^th
included)
var chkbxsnegate = function(n) {
var cbox = document.querySelectorAll('input[type="checkbox"][name^="chkbx"]');
while (n--) {
cbox[n].checked = !cbox[n].checked;
}
}
Example fiddle: http://jsfiddle/wHPnA/
You should use jQuery to inverse the checkboxes.
$('a#cb-inverse').click(function() {
$('input.my_cb').each(function() {
$(this).is(':checked') ? $(this).removeAttr('checked') : $(this).attr('checked','checked');
});
});
See this link for more information.
本文标签: javascriptNegate checkbox selection status on multiple checkboxesStack Overflow
版权声明:本文标题:javascript - Negate checkbox selection status on multiple checkboxes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741958994a2407158.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论