简体   繁体   English

JavaScript或jQuery来检查前20个复选框,然后是下20个,依此类推

[英]JavaScript OR jQuery to check first 20 checkboxes, then next 20 and so on

Is it possible to use JavaScript or jQuery to do the above? 是否可以使用JavaScript或jQuery完成上述操作?

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): 而我的复选框是使用PHP这样动态生成的(我有很多):

<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? 是否可以使用JavaScript或jQuery完成上述操作?

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. 如果您可以在jQuery中进行操作,则可以使用“普通” JavaScript进行操作,因为jQuery是用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/ 演示: 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... 这将获取已单击的特定链接的索引,并使用它来确定要检查的复选框的范围,然后检查那些复选框...

This JQUERY code will do it for you in simple way. 此JQUERY代码将以简单的方式为您完成此操作。

$('[name="select[]"]').slice(start,end).prop("checked",true);

EXAMPLES 例子

just use this function.. If you want to check first 20 then 只需使用此功能即可。如果要先检查20

$('[name="select[]"]').slice(0,20).prop("checked",true);

if you want to check 30 to 40 check box then use 如果要选中30到40复选框,请使用

$('[name="select[]"]').slice(30,40).prop("checked",true);

There's lots of ways to do this. 有很多方法可以做到这一点。

With your current HTML, the best way would simply be to iterate over the elements, and select them individually; 对于当前的HTML,最好的方法就是简单地遍历元素,然后分别进行选择;

function select(start, end) {
    for (var i=start;i<end;i++) {
        $('.checkbox[value="' + i + '"]').prop('checked', true);
    }
}

Although it would be much more efficient if you could add a unique ID to each of the elements (be aware that prior to HTML5, you couldn't start an ID with a number, so you might need to prefix it with something; 如果可以为每个元素添加唯一的ID,效率会大大提高(请注意,在HTML5之前,您不能以数字开头ID,因此可能需要在其前面加上一些前缀;

function select(start, end) {
    for (var i=start;i<end;i++) {
        document.getElementById("prefix_" + i).checked = true;
    }
}

In terms of how to match <a href=#>Check 1-20</a> to select(1,20) dynamically, the best way would be to add a common class to the hyperlinks, and use data attributes to set the minimum and max; 就如何动态匹配<a href=#>Check 1-20</a>select(1,20) ,最好的方法是向超链接添加一个通用类,并使用data属性来设置最小和最大

<a href="#" class="highlight" data-min="1" data-max="20">Check 1-20</a>

Then: 然后:

$(document).on('click', '.highlight', function (e) {
    event.preventDefault();

    var self = $(this);

    select(self.data('min'), self.data('max'));
});

You could also parse the text of the hyperlink manually if you wanted (and avoid data attributes; 您还可以根据需要手动解析超链接的文本(并避免使用data属性;

$(document).on('click', 'a', function (e) {
    event.preventDefault();

    var values = $(this).text().match(/ (\d+)-(\d+)/);

    select(parseInt(values[1], 10), parseInt(values[2], 10));
});

There is no need for a loop, you can just use the following where X is the start index and Y is the end index. 不需要循环,您可以使用以下代码,其中X是开始索引,Y是结束索引。

 $('input[name='select[]']:gt(X):lt(Y)').attr("checked", true); 

 $('input[name='select[]']:gt(0):lt(20)').attr("checked", true);

Turn this in to a function similar to Rory's post and it would be dynamic (without the looping). 将此功能转为与Rory的帖子类似的功能,它将是动态的(无循环)。

You can do it this way .. 你可以这样子..

<a href=# rel="1-20">Check 1-20</a>
<a href=# rel="21-40">Check 21-40</a>
<a href=# rel="41-60">Check 41-60</a>

I have added rel attribute to your links, and I will use it to find elements. 我已将rel属性添加到您的链接中,并将使用它来查找元素。

Attach click event: 附加点击事件:

$('#id_of_element_where_links_are a').click(function(){
    var rel = $(this).attr('rel').split('-');
    var start = parseInt(rel[0])-1;
    var end = parseInt(rel[1]);
     //i have start and end element now
     $('input[type="checkbox"]:eq('+start+')').attr('checked', 'checked');
    var ellementTill = $('input[type="checkbox"]:eq('+end+')');
    //now when I have end element I will check all from start to end
    $('input[type="checkbox"]:eq('+start+')').nextUntil(elementTill, "input").attr('checked','checked');

});

So what I have done, I have added rel attribute to links, then used them to get range: eg 1-20 is from 1 to 20. End I have decreased from by one because elements are indexed from 0. Then I have searched for end element and checked all elements till the end element. 所以我做了,我向链接添加了rel属性,然后使用它们来获取范围:例如1-20从1到20。结束由于元素从0开始索引,我从1减少了。然后我搜索了结束元素并检查所有元素,直到结束元素。 End element won't be checked that is the reason I didn't decreased 20 by 1. 不会检查End元素,这是我没有将20减1的原因。

You can try this : 您可以尝试以下方法:

$('a').click(function() {
    $('.checkbox:gt(' + ($(this).index() * 20) + '):lt(' + (($(this).index() * 20) +21) + ')').attr('checked', 'checked');
});

With this : 有了这个 :

<a href="#">Check 1-20</a>
<a href="#">Check 21-40</a>
<a href="#">Check 41-60</a>



<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"/>

I think this would work too: 我认为这也会起作用:

function checkOptions(from, to){
    var checksArray = $(".checkbox input[type=checkbox]");

    for(var i = from; i < to; i++){
        $(checksArray[i]).attr("checked", true);
    }

    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM