【问题标题】:Rails implementation of Two-level JQuery check and uncheck all child checkboxes of a parent checkbox两级JQuery的Rails实现检查和取消选中父复选框的所有子复选框
【发布时间】:2016-05-01 23:55:12
【问题描述】:

尝试实现多个(父/子)复选框集,其中“选中所有”选中所有复选框,取消选中其中一个子复选框将取消选中“选中所有”。在网上找到了几个解决方案,但没有一个使用 Ruby on Rails。一种看起来很有希望的解决方案使用了简单的 HTML 和 jQuery 脚本。基本的 HTML 看起来像这样——在 jQuery 代码中引用了这些类以识别复选框:

<html>
   <fieldset>
        <input type="checkbox" class="parentCheckBox" /> Parent 1<br />
        <input type="checkbox" class="childCheckBox" /> Child 1-1<br />
        <input type="checkbox" class="childCheckBox" /> Child 1-2<br />
        <input type="checkbox" class="childCheckBox" /> Child 1-3<br />
        <input type="checkbox" class="childCheckBox" /> Child 1-4<br />
        <input type="checkbox" class="childCheckBox" /> Child 1-5<br />
    </fieldset>
</html>

我在 Rails 中生成了一个简单的脚手架来测试想法,使用了一个父级和 3 个子级。通用脚手架代码如下所示:

<fieldset>

  <div class="field">
    <%= f.label :checkall %><br>
    <%= f.check_box :checkall %>
  </div>
  <div class="field">
    <%= f.label :date_one %><br>
    <%= f.check_box :date_one %>
  </div>
  <div class="field">
    <%= f.label :date_two %><br>
    <%= f.check_box :date_two %>
  </div>
  <div class="field">
    <%= f.label :date_three %><br>
    <%= f.check_box :date_three %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %> 

</fieldset>

Rails API 将其显示为复选框语法:

check_box(method, options = {}, checked_value = "1", unchecked_value = "0")

...并举个例子:

check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no")
 # => <input name="eula[accepted]" type="hidden" value="no" />
 #    <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />  

...但是我在这里抓住了稻草,因为我不确定定义、语法等。尝试将上面那个简单的 HTML 翻译成 ruby​​speak,我想出了这个:

<fieldset>

  <div class="parentCheckbox">
    <%= f.label :checkall %><br>
    <%= f.check_box :checkall, {class: parentCheckBox} %>
  </div>
  <div class="childCheckBox">
    <%= f.label :date_one %><br>
    <%= f.check_box :date_one, {class: childCheckBox} %>
  </div>
  <div class="childCheckBox">
    <%= f.label :date_two %><br>
    <%= f.check_box :date_two, {class: childCheckBox} %>
  </div>
  <div class="childCheckBox">
    <%= f.label :date_three %><br>
    <%= f.check_box :date_three, {class: childCheckBox} %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %> 

</fieldset>  

...并得到这个错误:

undefined local variable or method `parentCheckBox' for #<#<Class:0xbdcb5e4>:0xc07ab8c>
Extracted source (around line #18):
15 
16 <div class="parentCheckbox">
17 <%= f.label :checkall %><br>
18 <%= f.check_box :checkall, {class: parentCheckBox} %>
19 </div>

我敢肯定我在这方面做得不够好,但是 API 在将应该非常简单的 HTML 转换为嵌入式 ruby​​ 方面几乎没有帮助。我知道 jQuery 脚本适用于 HTML,但我需要提交表单的结果来提供数据库,而不仅仅是在显示中看起来很漂亮,所以我尽量不要过多地混淆 rails 生成的代码。

迷路了。

【问题讨论】:

    标签: jquery ruby-on-rails checkbox


    【解决方案1】:

    - 将 '' 添加到字符串 ('parentCheckBox')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 2021-12-02
      相关资源
      最近更新 更多