Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当表单通过文本字段提交时,如何在Rails中运行javascript代码?_Javascript_Html_Ruby On Rails_Forms_Haml - Fatal编程技术网

当表单通过文本字段提交时,如何在Rails中运行javascript代码?

当表单通过文本字段提交时,如何在Rails中运行javascript代码?,javascript,html,ruby-on-rails,forms,haml,Javascript,Html,Ruby On Rails,Forms,Haml,我在HAML中定义了一个可编辑文本字段表,如下所示: %table %tbody %tr %td -hints.each do |hint| =form_for(level_source_hint, :remote => true) do |f1| =text_field_tag 'message' + hint.id.to_s, level_source_hint.hint

我在HAML中定义了一个可编辑文本字段表,如下所示:

%table
  %tbody
    %tr
      %td
        -hints.each do |hint|
          =form_for(level_source_hint, :remote => true) do |f1|
            =text_field_tag 'message' + hint.id.to_s, 
               level_source_hint.hint, class: 'input-xxlarge'
      %td{:id => 'sibling' + hint.id.to_s} ...
请注意,没有“提交”按钮。如果有人编辑文本字段并按return,则会发送一个更新请求,该请求有效

当用户提交表单时,我想用javascript对同级节点执行一些操作(在第二个
%td
)。我已经尝试将
:onchange
属性添加到
form_for
标记中,但它没有显示在生成的html中

虽然我可能能够将函数绑定到每个不同的表单,但这样做会很难看,因为我需要为每一行创建不同的函数(直到运行时才知道函数的数量),并且每个函数都必须硬编码它来自哪一行(例如,它的
hint.id

我真的需要为每个表单创建和绑定一个函数,还是有更优雅的解决方案


我将Rails 4.0.3与jquery Rails 3.1.10和jquery ui Rails 4.2.0一起使用。

您实际上可以将绑定到其父元素,然后在事件冒泡到处理程序时检查它是哪个元素。类似于
$('#parent_id').on('change','.field class',function(){//您在这里处理提交的代码})

谢谢!您是建议一个绑定,还是每行一个?如果是前者,我如何判断哪一行的形式发生了更改?父元素上的一个绑定和子元素中的事件将弹出。您还可以绑定到调用的子窗体的
submit
事件。在处理程序中,如果您引用
$(this)
,它的作用域将限定为触发事件的元素。