gpt4 book ai didi

javascript - Rails Button_tag 与 Button_to - 类和 id

转载 作者:行者123 更新时间:2023-12-03 03:55:44 33 4
gpt4 key购买 nike

我正在尝试构建一个带有 id 的按钮和一个带有 Rails 的类。单击按钮时将触发 javascript 事件。我尝试了不同的方法,但没有一个能满足所有条件。

  <%= button_tag "Launch the js event", id: "button-feedback-by-bullet" %> 

这可行,但没有类

  <%= button_tag "Launch the js event", id: "button-feedback-by-bullet", class: "btn-primary" %> 

我收到一条错误消息,提示参数太多

  <%= button_to "Launch the js event", id: "button-feedback-by-bullet", class: "btn-primary" %>

我得到了正确的类,但按钮传递了 post 方法,并且 js 事件没有被触发

您能帮我找到正确的语法并理解button_tag 和button_to 之间的细微差别吗?谢谢。

最佳答案

button_tag(content_or_options = nil, options = nil, &block) public

你应该这样做

<%= button_tag "Launch the js event", {id: "button-feedback-by-bullet", class: "btn-primary"} %>

或使用button_to

button_to(name = nil, options = nil, html_options = nil, &block)

<%= button_to "Launch the js event", {}, {id: "button-feedback-by-bullet", class: "btn-primary"} %>

此外,button_tagbutton_to 默认情况下会触发submit,也许您应该使用 link_to 相反

关于javascript - Rails Button_tag 与 Button_to - 类和 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44958209/

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