admin管理员组文章数量:1287517
Here i have bunch of checkboxes,
say, if i clicked on any checkbox, i want make checked next two checkboxes also (i.e, beside two)
<table border="1" cellspacing="0" width="450">
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
</table>
Fiddle
could anyone help me out,
Thanks in advance.
Here i have bunch of checkboxes,
say, if i clicked on any checkbox, i want make checked next two checkboxes also (i.e, beside two)
<table border="1" cellspacing="0" width="450">
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" /></td>
</tr>
</table>
Fiddle
could anyone help me out,
Thanks in advance.
Share Improve this question asked Dec 6, 2013 at 11:36 user123456789user123456789 5663 gold badges12 silver badges31 bronze badges 10- 3 Naresh: Please include some attempted solutions, why they didn't work, and the expected results. – palaѕн Commented Dec 6, 2013 at 11:38
- In what order? Left to right? What should happen if the last one in a row is clicked? or simply the last checkbox? And what if one of the checkboxes is already checked, should it uncheck? – Teun Pronk Commented Dec 6, 2013 at 11:39
- 4 @NareshKamireddy Do you think programming is about googling and copying code? Programming is about learning how to use the tools and ing up with the solution yourself based on that understanding. – Barmar Commented Dec 6, 2013 at 11:40
-
2
@NareshKamireddy, You already asked this jQuery question
5 months ago
. Do you think you still new to jQuery? :) Its easy framework to learn – Murali Murugesan Commented Dec 6, 2013 at 11:41 - 1 Thought of adding an answer , but the experts advice will be void if I do that , Will not add an answer now.. @Murali Good Observation – Bharath R Commented Dec 6, 2013 at 11:44
10 Answers
Reset to default 4try with nextAll()
and eq
to select next two.
$('input:checkbox').change(function(){
var obj=$(this).parent().nextAll(':eq(0),:eq(1)'); //eq(0) and (1) is the index of next two checkbox
obj.find(':checkbox').prop('checked',this.checked)
});
fiddle here
this uncheck the checkbox if unchecked..
$(":checkbox").click(function () {
$(this).parent().nextAll("td").slice(0, 2).find(":checkbox").prop('checked', true);
});
JSFIDDLE
Try this it has toggle effect as you want.
Here is the script:
$(':checkbox').click(function () {
if ($(this).is(":checked")) {
$(this).parent().nextAll("td").slice(0, 2).find(":checkbox").prop('checked', true);
}
else {
$(this).parent().nextAll("td").slice(0, 2).find(":checkbox").prop('checked', false);
}
});
Here is the working DEMO
Or try this
var len = 2;
var selector = 'input:checkbox';
$(selector).click(function(){
var e = $(selector).index(this);
$(selector+':lt('+(e+len+1)+'):gt('+e+')')
.attr('checked', 'checked');
});
JSFiddle
Try this
I have added button on each tr
$('button').on('click', function () {
$(this).parents('tr').find('td>input[type=checkbox]').each(function (i, j) {
if (i == 3) {
return false;
}
$(this).prop('checked', true);
})
})
JSFiddle
Vanilla JS solution:
var checkboxes = document.querySelectorAll('input[type="checkbox"]');
for(var i=0,iLen=checkboxes.length; i<iLen; i++){
(function(i){
checkboxes[i].addEventListener('click',function(){
checkboxes[(i+1)%checkboxes.length].checked = true;
checkboxes[(i+2)%checkboxes.length].checked = true;
})
}(i))
}
JSFiddle
A different approach. Not using the DOM, instead I'm using the array of the input elements.
Also works for first and last element (you didn't specify if you wanted this).
http://jsfiddle/Y7tR2/
$('input').change(function () {
var i = $.inArray(this, $('input'));
var n = i + 1;
var p = i - 1;
if (n >= $('input').length) n = 0;
if (p < 0) p = $('input').length - 1;
$('input').eq(n).attr("checked", $(this).attr('checked') ? true : false);
$('input').eq(p).attr("checked", $(this).attr('checked') ? true : false);
});
Try this working script
$('input[type="checkbox"]').click(function(obj){
if( $(this).is(':checked')) {
$(this).parent().nextAll().has(":checkbox").eq(0).find(":checkbox").attr('checked','checked');
$(this).parent().nextAll().has(":checkbox").eq(1).find(":checkbox").attr('checked','checked');
}else{
$(this).parent().nextAll().has(":checkbox").eq(0).find(":checkbox").removeAttr('checked');
$(this).parent().nextAll().has(":checkbox").eq(1).find(":checkbox").removeAttr('checked');
}
});
DEMO
$(':checkbox').click(function(){
$(this).parent().nextAll().slice(0,2).find(":checkbox").attr('checked',this.checked)
})
another solution using jquery:
$('input').on('click', function () {
var count=0;
$.each($(this).parents('tr').find('td>input[type=checkbox]'), function( index, value ) {
if($(value).is(":checked")){
count++;
} else if(count > 0 && count <= 2){
$(value).attr('checked', true);
count++;
}
});
})
JSFiddle
本文标签: javascripthow to make a checkboxes(3) checked at one click using jqueryStack Overflow
版权声明:本文标题:javascript - how to make a checkboxes(3) checked at one click using jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741278517a2369870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论