gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 05:08:54 24 4
gpt4 key购买 nike

我需要将提交按钮替换为“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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com