JavaScript或jQuery来检查前20个复选框,然后是下20个等等 [英] JavaScript OR jQuery to check first 20 checkboxes, then next 20 and so on
问题描述
是否可以使用JavaScript或jQuery来执行上述操作?
我想像一个链接,例如:
< a href =#>检查1-20< / a>
< a href =#>检查21-40< / a>
< a href =#>检查41-60< / a>
等等。
我的复选框动态生成PHP像这样(我有很多):
< input type =checkbox class =checkboxname =select []value =5153/>
< input type =checkboxclass =checkboxname =select []value =5154/>
< input type =checkboxclass =checkboxname =select []value =5155/>
如果任何人有任何想法或最好的方式去做,
是否可以使用JavaScript或jQuery执行上述操作?
是的。有很多方法可以做到。如果你可以在jQuery中做到这一点,你可以在纯JavaScript中做到这一点,因为jQuery是用JavaScript编写的。
这里有一种方法:
$(document).ready(function(){
var $ cbs = $('input:checkbox [name =select [ ]'),
$ links = $(a); //你可能不想要_all_ links,
//所以添加一个类或者什么东西
$ links。单击(function(){
var start = $ links.index(this)* 20,
end = start + 20;
$ cbs.slice(start,end) checked,true);
});
}); =nofollow> http://jsfiddle.net/nnnnnn/Q6SxW/
这会获取被点击的特定链接的索引,并使用它要找出要检查的复选框的范围,然后检查这些复选框...
Is it possible to use JavaScript or jQuery to do the above?
I'm imagining a link for each, something like:
<a href=#>Check 1-20</a>
<a href=#>Check 21-40</a>
<a href=#>Check 41-60</a>
and so on..
whilst my checkboxes are generated dynamically with PHP like this (i have a lot of them):
<input type="checkbox" class="checkbox" name="select[]" value="5153"/>
<input type="checkbox" class="checkbox" name="select[]" value="5154"/>
<input type="checkbox" class="checkbox" name="select[]" value="5155"/>
If anyone has any ideas or best way to go about this, it'll be greatly appreciated :)
解决方案
Is it possible to use JavaScript or jQuery to do the above?
Yes. There are lots of ways to do it. And if you can do it in jQuery you can do it in "plain" JavaScript given that jQuery is written in JavaScript.
Here's one way:
$(document).ready(function() {
var $cbs = $('input:checkbox[name="select[]"]'),
$links = $("a"); // you probably don't want _all_ links,
// so add a class or something.
$links.click(function() {
var start = $links.index(this) * 20,
end = start + 20;
$cbs.slice(start,end).prop("checked",true);
});
});
Demo: http://jsfiddle.net/nnnnnn/Q6SxW/
This takes the index of the particular link that was clicked and uses it to figure out the range of checkboxes to check, then it checks those checkboxes...
这篇关于JavaScript或jQuery来检查前20个复选框,然后是下20个等等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!