javascript - 提交方法在 Rails 中不起作用

标签 javascript jquery ruby-on-rails coffeescript

我需要将提交按钮替换为“link_to”。

#View
<%= form_for :task, remote: true, id: 'create_form' do |f| %>
  ....
  <%= link_to 'OK', '', id: 'submit_link' %>
<% end %>


#CoffeeScript
ready = ->
  $("#submit_link").click (event) ->
    $("#create_form").submit()
$(document).ready ready
$(document).on "page:load", ready

点击触发器工作正常,但“submit()”方法不起作用,我不知道为什么。 我还尝试添加“event.preventDefault()”,但没有任何改变。

最佳答案

我认为你需要像这样设置表单的ID

<%= form_for :task, remote: true, html: { id: 'create_form' } do |f| %>

http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

或者,只需使用 form_for 生成的表单 ID,该 ID 应为 new_task

#CoffeeScript
ready = ->
  $("#submit_link").click (event) ->
    $("#new_task").submit()
$(document).ready ready
$(document).on "page:load", ready

关于javascript - 提交方法在 Rails 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41912382/

相关文章:

javascript - 在一页上实现 2 个 Bootstrap 轮播?

javascript - IE+jQuery+Ajax+XHTML : HTML getting clipped after . html() 或 .innerHTML

javascript - 表单验证失败后 jQuery 不起作用

ruby-on-rails - 我应该如何安排我的模型和 cron rake 任务以按不同的开始日期间隔发送电子邮件?

html - ruby rails : Syntax within CSS in view?

javascript - 如何使用 JSDoc 记录 useState Hook ?

javascript - IBM Watson Visual Recognition - 由于凭证无效,访问被拒绝

javascript - Kendo图表数据标签格式

jquery - 如何将 CSS 类名传递给 jQuery 自动完成组合框?

javascript - 未捕获的安全错误 : Blocked a frame with origin "https://www.google.com" from accessing a frame with origin "http://my.site.com".