Rails:将表单字段值添加到数据确认消息?

时间:2016-05-12 09:21:22

标签: javascript ruby-on-rails forms ruby-on-rails-4

如何将用户输入的表单数据添加到数据确认消息中?

= form_for(@booking, html: { class: "form-horizontal", role: "form" }) do |f|

    .form-group.emails
      = f.label :emails,'Any other emails to add?', class: "col-sm-2 control-label"
      .col-sm-10
        = f.text_field :emails, class: "form-control", placeholder: 'email address separated by a comma'       

.form-group
  .text-center
  = f.submit "Send dates",
  data: { confirm: "Are these email addresses correct? {add email addresses here..}" }, class: "btn btn-primary"

我想添加用户在电子邮件字段中输入的电子邮件。 因此,如果用户输入地址 user@example.com ,确认信息将是

  

"这些电子邮件地址是否正确? user@example.com"

2 个答案:

答案 0 :(得分:2)

$("#email_fields").change(function(){
    $('.email-button input').data('confirm', "Are these email addresses correct?  " + $("#email_fields").val())
  });

答案 1 :(得分:0)

使用jquery&将ID分配给您的字段,如

= f.text_field :emails, class: "form-control", placeholder: 'email address separated by a comma', id: "email_fields" 

= f.submit "Send dates",id: "submit_button"

然后

$(document).ready(function() {
    $("#submit_button").click(function(event) {
        if( !confirm('Are these email addresses correct?'+ $("#email_fields").val() ) 
            event.preventDefault();
    });
});