简体   繁体   English

使用向导表单中的按钮提交值的最佳方法

[英]Best way to submit a value with a button in a wizard form

I've been trying to create a wizard form that is basically a series of Yes or No questions. 我一直在尝试创建一个基本上是一系列是或否问题的向导表单。 There's also a couple pages in the form where the user will have to select multiple options before continuing. 表格中还有几页,用户在继续之前必须选择多个选项。

Right now all the object parameters are mostly booleans but all the wizard examples I've seen involve basic forms (text_fields, check_box's, etc.). 现在所有的对象参数都是布尔值,但我见过的所有向导示例都涉及基本形式(text_fields,check_box等)。

I figured I might be able to do something like this: 我想我可能会做这样的事情:

<div class="button1">
    <%= f.hidden_field :newsletter, value: "1" %>
    <%= f.submit "Yes" %>
</div>

<div class="button2">
    <%= f.hidden_field :newsletter, value: "0" %>
    <%= f.submit "No" %>
</div>

But it doesn't seem to be saving the value. 但它似乎没有节省价值。 I'm using the wicked gem for the wizard and am following this RailsCast tutorial 我正在使用邪恶的宝石为向导,并遵循这个RailsCast教程

Note: Normal fields like text_fields save, just not these button booleans. 注意:普通字段如text_fields保存,而不是这些按钮布尔值。

Both those buttons, and the hidden fields, are all in the same form. 这些按钮和隐藏字段都具有相同的形式。 So, either button will submit the form, and when the form submits it will gather the values of all the fields into params. 因此,任何一个按钮都会提交表单,当表单提交时,它会将所有字段的值收集到params中。

What you can do is to make the buttons themselves into an input, sending through a param. 你可以做的是将按钮本身变成一个输入,通过一个参数发送。 This is done with "name" and "value" attributes, like any other attribute: 这与“name”和“value”属性一样,与任何其他属性一样:

<input type="submit" value="foo" name="bar" />
<input type="submit" value="chunky" name="bacon" />

Now if you submit with the first button you will get :bar => "foo" in params and if you submit with the second you'll get :bacon => "chunky" . 现在,如果你使用第一个按钮提交,你将获得:bar => "foo" in params如果你提交第二个,你将得到:bacon => "chunky"

The conventional name is "commit". 传统的名称是“提交”。 When you use a rails submit_tag, it will set "name" to "commit" and it will set "value" to the text of the button. 使用rails submit_tag时,它会将“name”设置为“commit”,并将“value”设置为按钮的文本。 So, you should see :commit => "Yes" or :commit => "No" in params, and change your logic accordingly. 因此,您应该在params中看到:commit => "Yes":commit => "No" ,并相应地更改您的逻辑。

It's better to use jQuery to trigger an event and add the hidden input accordingly 最好使用jQuery触发事件并相应地添加隐藏的输入

<div class="button1">
  <%= f.submit "Yes", class: "wizard-btn" %>
</div>

<div class="button2">
  <%= f.submit "No", class: "wizard-btn" %>
</div>
... ...

sample coffeescript code would be like below: 示例coffeescript代码如下所示:

jQuery ->
  $(".wizard-btn").on "click", (e) ->
    e.preventDefault()
    thisForm = $(".wizard-form")
    hideDiv = "<input type=\"hidden\" name=\"newsletter\" />"
    if $(this).val() == "Yes"
      thisForm.append $(hideDiv).val("1")
    else if $(this).val() == "No"
      thisForm.append $(hideDiv).val("0")
    thisForm.get(0).submit()

where I assume your form has class named: wizard-form 我假设你的表单有类命名为: wizard-form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM