admin管理员组文章数量:1356413
I want to stop the user to check another checkbox after a certain number of checkboxes have been checked already. i.e. After 3 checkboxes are checked, the user cannot check anymore and a message says 'You're not allowed to choose more than 3 boxes.'
I'm almost there but the last checkbox is still being checked and I don't want that, I want it to be unchecked with the message appearing.
How do I do that:
var productList = $('.prod-list'),
checkBox = productList.find('input[type="checkbox"]'),
pareList = $('pare-list ul');
productList.delegate('input[type="checkbox"]', 'click', function () {
var thisElem = $(this),
thisData = thisElem.data('pare'),
thisImg = thisElem.closest('li').find('img'),
thisImgSrc = thisImg.attr('src'),
thisImgAlt = thisImg.attr('alt');
if (thisElem.is(':checked')) {
if ($('input:checked').length < 4) {
pareList.append('<li data-paring="' + thisData + '"><img src="' + thisImgSrc + '" alt="'+ thisImgAlt +'" /><li>');
} else {
$('input:checked').eq(2).attr('checked', false);
alert('You\'re not allowed to choose more than 3 boxes');
}
} else {
var pareListItem = pareList.find('li');
for (var i = 0, max = pareListItem.length; i < max; i++) {
var thisCompItem = $(pareListItem[i]),
paringData = thisCompItem.data('paring');
if (thisData === paringData) {
thisCompItem.remove();
}
}
}
});
I want to stop the user to check another checkbox after a certain number of checkboxes have been checked already. i.e. After 3 checkboxes are checked, the user cannot check anymore and a message says 'You're not allowed to choose more than 3 boxes.'
I'm almost there but the last checkbox is still being checked and I don't want that, I want it to be unchecked with the message appearing.
How do I do that:
var productList = $('.prod-list'),
checkBox = productList.find('input[type="checkbox"]'),
pareList = $('.pare-list ul');
productList.delegate('input[type="checkbox"]', 'click', function () {
var thisElem = $(this),
thisData = thisElem.data('pare'),
thisImg = thisElem.closest('li').find('img'),
thisImgSrc = thisImg.attr('src'),
thisImgAlt = thisImg.attr('alt');
if (thisElem.is(':checked')) {
if ($('input:checked').length < 4) {
pareList.append('<li data-paring="' + thisData + '"><img src="' + thisImgSrc + '" alt="'+ thisImgAlt +'" /><li>');
} else {
$('input:checked').eq(2).attr('checked', false);
alert('You\'re not allowed to choose more than 3 boxes');
}
} else {
var pareListItem = pareList.find('li');
for (var i = 0, max = pareListItem.length; i < max; i++) {
var thisCompItem = $(pareListItem[i]),
paringData = thisCompItem.data('paring');
if (thisData === paringData) {
thisCompItem.remove();
}
}
}
});
Share
Improve this question
edited Nov 23, 2011 at 23:47
Jonathan Leffler
756k145 gold badges951 silver badges1.3k bronze badges
asked Nov 9, 2011 at 14:45
ShaozShaoz
10.7k26 gold badges75 silver badges100 bronze badges
2
- Could you share your HTML code? – Salman Commented Nov 9, 2011 at 14:49
- Oh, maybe I misunderstood the question... you want that the new checkbox is selected but the previously or first selected one gets deslected? – Felix Kling Commented Nov 9, 2011 at 14:57
2 Answers
Reset to default 9I might have misunderstood the question... see my ment.
Too prevent the selection, you can call event.preventDefault()
and define the handler with the event
parameter.
$('input[type="checkbox"]').click(function(event) {
if (this.checked && $('input:checked').length > 3) {
event.preventDefault();
alert('You\'re not allowed to choose more than 3 boxes');
}
});
DEMO
Alternatively, set this.checked
to false
. This will even prevent the browser from rendering the checkmark.
DEMO
one single jquery function for multiple forms
<form>
<input type="checkbox" name="seg"><br>
<input type="checkbox" name="seg" ><br>
<input type="checkbox" name="seg"><br>
<input type="checkbox" name="seg"><br>
</form>
<br><br><br><br><br>
<form>
<input type="checkbox" name="seg1"><br>
<input type="checkbox" name="seg1" ><br>
<input type="checkbox" name="seg1"><br>
<input type="checkbox" name="seg1"><br>
</form>
$('input[type="checkbox"]').click(function(event) {
if ($("input[name= "+ this.name +"]:checked").length > 3) {
event.preventDefault();
alert('You\'re not allowed to choose more than 3 boxes');
}
});
本文标签:
版权声明:本文标题:Stop checking checkboxes after a number of checkboxes have been checked in jQuery or JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744046886a2581689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论