简体   繁体   English

如何在rails中提取输入ID以在javascript中使用它

[英]How to extract input ID in rails to use it in javascript

I'm trying to show text input field depending on checkbox checked value.我正在尝试根据复选框选中的值显示文本输入字段。 Rails form helpers generate inputs with automaticly set ids. Rails 表单助手使用自动设置的 id 生成输入。 How can I refer to them in javascript.我如何在 javascript 中引用它们。

Fragment of rails form轨道形式的片段

<%= f.check_box_without_bootstrap :standard_product%>
<%= f.label(:standard_product, "Standard product" %>
<%= f.text_field :product_comment%>

Javascript function Javascript 函数

<script type="text/javascript">
  var checkbox = document.getElementById('????');
  var details_div = checkbox.nextSibling.nextSibling;
  checkbox.onchange = function() {
    if(this.checked) {
      details_div.style['display'] = 'block';
    } else {
      details_div.style['display'] = 'none';
    }
  };
</script>

Could you please suggest solution (that is not hard setting id for checkbox)?您能否提出解决方案(这不是复选框的硬设置 ID)? Thanks :-)谢谢 :-)

Rails check_box helper is defined as: Rails check_box helper 定义为:

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

so in your code you can pass id in the options所以在你的代码中你可以在选项中传递 id

Thank you very much.非常感谢。 I just copied generated id from inspector...我刚刚从检查员那里复制了生成的 ID...

I wish to know how to point to id generated by ruby helper f.check_box in <script> tag using <%= ruby %> :-)我想知道如何使用<%= ruby %> f.check_box <script>标签中由 ruby​​ helper f.check_box生成的id :-)

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

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