【问题标题】:Triggering a click through Jquery on ruby code通过 Jquery 在 ruby​​ 代码上触发点击
【发布时间】:2015-01-30 05:37:47
【问题描述】:

所以我试图让我的f.submit 在我点击f.check_box 时被触发。我不知道您是否可以使用 jquery 来处理 ruby​​... 但基本上这是一个包含页面上所有项目的待办事项列表的视图。当我单击复选框将其标记为关闭时,我希望它自动提交。

我想我可以在f.check_boxf.submit 上分配一个div 标签来触发它们...这是我使用的第一个JQuery。

<h1><%= @list.title %></h1>
     <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <div>

    <h2><% @list.items.each do |item| %></h2>
    <%= item.content %>
      <%= form_for([@list, item]) do |f| %>
      <div id="target">
        <%= f.check_box :complete %>
      </div>
        <div id="handler">
          <%= f.submit %>
          <% end %>
          </div>
    <% end %>
     </div>
    <script>
      jQuery(document).ready(function() {

        jQuery("#target").click(function() {
          jQuery("#handler").click();
      });
     });

    </script>




    <% if policy(@list).update? %>
      <%= link_to "Edit List", edit_list_path, class: 'btn btn-success' %>
      <% end %>



     <div class="col-md-4">
       <% if policy(Item.new).create? %>
         <%= link_to "New Item", new_list_item_path(@list), class: 'btn btn-success' %>
       <% end %>

       <% if policy(@list).destroy? %>
          <%= link_to "Delete List", @list, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to destroy this lovely list?'} %>
          <% end %>
     </div>
    </div>



    <%= render "items/form" %>




    <!-- 
    new_list_item_path

    /lists/5/items/new
     -->

【问题讨论】:

    标签: javascript jquery html ruby-on-rails ruby


    【解决方案1】:

    div 不会触发onclick 事件,您需要设置checkboxonchange 方法,该方法将提交表单。

    这是一个代码。 您需要为每个提交按钮设置不同的id,然后您可以使用 Jquery 提交该表单。

    我设置了复选框的 onchange 方法,该方法根据项目 ID 提交表单。

    <h1><%= @list.title %></h1>
         <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <div>
    <%index=0%>
    <h2><% @list.items.each do |item| %></h2>
    <%= item.content %>
      <%= form_for([@list, item]) do |f| %>
        <%=f.check_box :complete,{:onchange=> "submit_form("+@list.items[index].id.to_s+");"}%>
          <%= f.submit :id=>@list.items[index].id%>
           <%index+=1%>
          <% end %>
    <% end %>
     </div>
    
    
    
    
    
    <% if policy(@list).update? %>
      <%= link_to "Edit List", edit_list_path, class: 'btn btn-success' %>
      <% end %>
    
    
    
     <div class="col-md-4">
       <% if policy(Item.new).create? %>
         <%= link_to "New Item", new_list_item_path(@list), class: 'btn btn-success' %>
       <% end %>
    
       <% if policy(@list).destroy? %>
          <%= link_to "Delete List", @list, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to destroy this lovely list?'} %>
          <% end %>
     </div>
    </div>
    
    
    
    <%= render "items/form" %>
    
    
    
    <script>
        function submit_form(id) {
             $('#'+id).click();
        }
    
    </script>
    

    【讨论】:

      【解决方案2】:

      嗯,试试这样的:

      $("input[type=checked]").on("click", function() {
        if( $(this).is(':checked') ) { $('form').submit() }
      }
      

      你可能想要

      <%= f.check_box :complete, html: { class: "check_complete"} %>
      

      <%= f.submit, ".." id: "handler"%>
      

      然后将 input[type=checked] 替换为 .check_complete 并可能使用 ("#handler").click(); 而不是 .submit()

      【讨论】:

      • hmm.... 尝试了这两个建议,但仍然没有。我不得不让我的 f.submit 保持原样,添加 id:handler 给我一个语法错误。所以我仍然把它标记为&lt;div id="handler"&gt; &lt;%= f.submit %&gt;。 @argentum47
      • @BartDangus 为什么会抛出语法错误!!显示代码!!应该像 ,像这里stackoverflow.com/questions/12488051/… 和U 使用的是rails 4 或更高版本?
      猜你喜欢
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-14
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      相关资源
      最近更新 更多