简体   繁体   English

在循环中使用Jquery选择/取消选择多个复选框(Rails)

[英]Select/Deselect multiple check boxes using Jquery in loop(Rails)

I working on RoR, I have a two loops inside that i am displaying records with check boxes. 我在RoR上工作,里面有两个循环,显示带有复选框的记录。

<%= form_tag some_path, :method => 'post' do %>
  For loop
    <%= check_box_tag 'ids[]', value %> Name
    For loop
      <%= check_box_tag 'ids_of_second_loop[]', value %> Name
    end
  end
<%= submit_tag "Next" %>
<% end %>

Now i wanted to apply a jquery where i can select/ deselect the checkboxes. 现在我想应用一个jQuery,我可以在其中选择/取消选择复选框。

I can do if its not in the loop straight forward, 如果它不在循环中,我可以做,

I am referring below link for implementation, link 我指的是下面的链接实现, 链接

Loop Output... 回路输出...

在此处输入图片说明

How to achieve it... 如何实现...

I would do something like 我会做类似的事情

<%= form_tag some_path, :method => 'post' do %>
  For loop
    <%= check_box_tag 'ids[]', value, class: 'parent' %> Name
    <div class='children'>
      For loop
        <%= check_box_tag 'ids_of_second_loop[]', value %> Name
      end
    </div>
  end
<%= submit_tag "Next" %>
<% end %>

note that "div class='children'" after the checkbox and the class "parent" on all parent checkboxes 请注意,该复选框之后为“ div class ='children'”,而所有父复选框均为“ parent”类

then, javascript 然后,javascript

$('input.parent').on('change',function(){
  $(this).next('.children').find('input[type=checkbox]').prop('checked',$(this).prop('checked'));
})

it searches the next div with class 'children' after the clicked checkbox, then, it searches all inputs with type=checkbox inside that div, and sets the prop "checked" to the value returned by the current input clicked 它会在单击的复选框之后搜索类别为“孩子”的下一个div,然后,在该div中搜索所有type = checkbox的输入,并将属性“ checked”设置为当前单击的输入返回的值

Working fiddle : 工作提琴:

$("#button").click(function () {    
    if($("#test")[0].checked)
    {
        $("#test")[0].checked = false;
    }
    else
    {
        $("#test")[0].checked = true;
    }

});

http://jsfiddle.net/q8wvv1ky/ http://jsfiddle.net/q8wvv1ky/

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

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