简体   繁体   English

使用Javascript选择/取消选择Rails 4中的所有动态复选框

[英]Select/Deselect All dynamic checkboxes in Rails 4 with Javascript

I have followed this StackOverflow Question's Answer1 and Answer2 我按照这个StackOverflow的问题的正确答案为ANSWER2

but not getting result.. As I am calling all checkbox list's data from Range tabel. 但没有得到结果。当我从Range表中调用所有复选框列表的数据时。 and their id is also defined by data.. Let me show you the code for this screenshot 并且它们的ID也是由数据定义的。让我向您展示此屏幕截图的代码

在此处输入图片说明

= f.collection_check_boxes :range_ids, Range.active, :id, :name, {},:class=>'checkbox'

which returns as 返回为

<input id="campaign_range_ids_1" class="checkbox" type="checkbox" value="1" name="campaign[range_ids][]">
<input id="campaign_range_ids_2" class="checkbox" type="checkbox" value="2" name="campaign[range_ids][]">
<input id="campaign_range_ids_3" class="checkbox" type="checkbox" value="3" name="campaign[range_ids][]">
<input id="campaign_range_ids_4" class="checkbox" type="checkbox" value="4" name="campaign[range_ids][]">

I want if All means id="campaign_range_ids_4" is selected/deselected then all other check-boxes should be perform accordingly.. 我想如果选择/取消选择All意味着id="campaign_range_ids_4"则所有其他复选框也应相应执行。

Here also have a screenshot of Range Table 这里还有范围表的屏幕截图 范围表

I had tried this Javascript 我曾经尝试过这个Javascript

:javascript
  $('#campaign_range_ids_4').click(function() {
       if(this.checked) {
           $('#campaign_range_ids_1').checked = true;
           $('#campaign_range_ids_2').checked = true;
           $('#campaign_range_ids_3').checked = true;
           });
       } else {
           $('#campaign_range_ids_1').checked = false;
           $('#campaign_range_ids_2').checked = false;
           $('#campaign_range_ids_3').checked = false;
           });
       }
    });

Please correct me where I do Mistake.... Thanks in advance.. :) 请纠正我在哪里做错的事...。在此先感谢.. :)

Updated Question 更新的问题

I have followed Shridhar's and Pavan's answer it works perfectly but now I want that when I select All then deselect any one then it must uncheck "All" (4th checkbox) but it remains as it is.. 我遵循ShridharPavan的回答,它的工作原理非常完美,但是现在我希望当我选择All时,再取消选择任何一个,然后必须取消选中“ All”(第4个复选框),但它保持原样。

As i said,it should be $('#campaign_range_ids_4') not $('#campaign_beacon_range_ids_4') 如我所说,应该是$('#campaign_range_ids_4')而不是$('#campaign_beacon_range_ids_4')

This will work too 这也将工作

$('#campaign_range_ids_4').click(function() {
   if(this.checked) {
       $(':checkbox').each(function() {
           this.checked = true;                        
       });
   } else {
      $(':checkbox').each(function() {
           this.checked = false;                        
       });
   } 
});

Demo 演示版

Error in Selectors ,It should be $('#campaign_range_ids_4') not $('#campaign_beacon_range_ids_4') use prop() to set the checkbox state 选择器错误,应为$('#campaign_range_ids_4')而非$('#campaign_beacon_range_ids_4')使用prop()设置复选框状态

Try this 尝试这个

$('#campaign_range_ids_4').click(function () {

    if ($(this).is(':checked')) {
        $('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').prop('checked', true);

    } else {
        $('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').prop('checked', false);
    }

});

DEMO 演示

OR 要么

$('#campaign_range_ids_4').click(function () {
    $('#campaign_range_ids_1, #campaign_range_ids_2, #campaign_range_ids_3').prop('checked', $(this).is(':checked'));
});

DEMO 演示

change the selector to $('#campaign_range_ids_4') and do this: 将选择器更改为$('#campaign_range_ids_4')并执行以下操作:

$('#campaign_range_ids_4').change(function() {
    $('#campaign_range_ids_1, #campaign_range_ids_2, #campaign_range_ids_3').prop('checked', this.checked);
});

instead of click use change method. 而不是click使用change方法。

Modifying Sridhar's answer finally I got the Answer of my Updated Question's. 最终修改了Sridhar的答案,我得到了我更新的问题的答案。

I wanted if All (4th checkbox) is checked then all check-box should be selected and if it's unchecked then all check-box should be unchecked it works perfectly 我想,如果All (4复选框)被checked ,那么所有复选框应该被选中,如果它unchecked ,那么所有复选框,应unchecked它完美

But Issue I had found that If All (4th checkbox) is selected then it check all boxes then if I unchecked any check-box then All (4th checkbox) must be uncheked which was not occur. 问题我已经发现,如果All (4复选框)被选中,那么它检查所有箱子然后如果我未选中任何复选框,然后All (4复选框)必须uncheked这是不会发生。

Solution by Modifying Code: In future if other people face the same issue.. 修改代码后的解决方案:将来如果其他人遇到相同的问题。

HTML Code: HTML代码:

<input id="campaign_range_ids_1" class="checkbox" type="checkbox" value="1" name="campaign[range_ids][]">London
<input id="campaign_range_ids_2" class="checkbox" type="checkbox" value="2" name="campaign[range_ids][]">India
<input id="campaign_range_ids_3" class="checkbox" type="checkbox" value="3" name="campaign[range_ids][]">USA
<input id="campaign_range_ids_4" class="checkbox" type="checkbox" value="4" name="campaign[range_ids][]">All

JavaScript: JavaScript:

$('#campaign_range_ids_4').click(function () {

    if ($(this).is(':checked')) {
        $('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').prop('checked', true);

    } else {
        $('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').prop('checked', false);
    }

});

$('#campaign_range_ids_1,#campaign_range_ids_2,#campaign_range_ids_3').click(function () {

    if ($(this).is(':checked')) {
        } else {
            $('#campaign_range_ids_4').prop('checked', false);
    }

});

Working Demo: 工作演示:

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

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