【问题标题】:Submit form using external button使用外部按钮提交表单
【发布时间】:2015-08-12 14:21:53
【问题描述】:

在表单外部的视图中,我有一个“更新”按钮。当我单击按钮时没有任何反应。请帮忙。非常感谢。

(haml)
  %button#update_button(type="submit) Update

The call for the form:
  = form_for @employee, id: 'employee-update-form' do |f|
    ...

And the JS intended to submit the form:
:javascript
  $('#update_button').click(function() {
    $('#employee-update-form').submit(function() {
      $.ajax({
        type: "PATCH",
        url: '#{employee_path(@employee)}'
        data: $('#employee-update-form').serialize(),
        dataType: "JSON"
      }).success(function(json) {
      });
      return false; // prevents normal behavior
    });

【问题讨论】:

    标签: jquery ruby-on-rails haml


    【解决方案1】:

    您已将 ajax 请求绑定到提交事件的表单,您仍然需要调用 submit() 来触发该事件。

    见 jsfiddle:http://jsfiddle.net/ma9kLm34/

    绑定调用后触发提交。

    $('#employee-update-form').submit();
    

    【讨论】:

      【解决方案2】:

      在表单上调用提交方法

      $('#employee-update-form').submit();
      

      【讨论】:

      • 它不是提交表单,它就像在提交时运行该功能,所以你需要超级调用提交方法我想是这样
      • 不是 ajax 解决方案,但它适用于正常行为。谢谢。
      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2022-01-03
      相关资源
      最近更新 更多