【问题标题】:rails partial does not generate eventsrails partial 不生成事件
【发布时间】:2015-08-24 21:06:41
【问题描述】:

我有一个通过部分呈现的表单。当表单中可供选择的可用字段被此表单中的提交事件或另一个表单中的删除事件更改时,需要重新绘制此表单。无论重绘表单的原因是什么,都不会检测到任何事件,在重绘/渲染表单之前,我可以在控制台输出中注册提交按钮。

向控制台生成日志消息的代码 - 在动态呈现表单之前一直有效。

$(document).ready(function(){
  console.log("On Ready")
  $(document).on('submit', '.ioConnect', function(event){
    console.log("Button Clicked");
    event.preventDefault();
  })
})

这是在部分 _links.html.erb 中呈现的表单

<table style="border: 1px solid;">    
    <tr>    
      <th>Create New Link</th>    
    </tr>

    <% @vehicle_configuration.recursive_outputs_array.each do |output| %>    
      <tr>    
        <%= form_for InputToOutputLink.new, :html => { class: 'ioConnect'} do |f| %>
          <td>Link </td>
          <td><%= output.friendly_name %><%= f.hidden_field :output_id, {:value => output.id} %></td>
          <td> to </td>
          <td><%= f.collection_select :input_id, @vehicle_configuration.unused_inputs_hash_for_output_type(output), :first, :last , { class: 'outputConnectionSelection' } %></td>
          <td>
            <%= f.hidden_field :vehicle_configuration_id, {:value => @vehicle_configuration.id} %>
            <%= f.submit "Connect", :html => { class: 'ioConnectSubmit'} %>
          </td>
        <% end %>
      </tr>
    <% end %>
</table>

这是导致表单在创建或删除链接时重绘的行

$("#create-links-container").html("<%= j( render('/vehicle_designer/links')).html_safe %>");

我确定这与正在重绘的表单以及重新连接事件的需要有关,但我不知道该怎么做。我认为通过收听未重绘的父元素上的提交会起作用,这就是我认为我在上面显示的第一个代码 sn-p 中所做的。

我遗漏了什么或做错了什么?

【问题讨论】:

    标签: javascript jquery ruby-on-rails forms partial


    【解决方案1】:

    尝试“ajax:before”、“ajax:success”等事件,因为表单是jquery-ujs提交的 https://github.com/rails/jquery-ujs/wiki/ajax

    【讨论】:

    • 感谢您的想法,但我看不出这有什么帮助,因为在重绘部分后 jquery-ujs 似乎没有再次触发。我试图在控制台中捕获按钮单击的唯一原因是为了证明它是否有效。查看日志,当我允许 form_for 中的 remote: true 选项时,没有任何内容提交给服务器
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2016-12-21
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 2020-01-29
    • 1970-01-01
    • 2011-04-29
    相关资源
    最近更新 更多