【问题标题】:Rails: add form field value to data confirm message?Rails:将表单字段值添加到数据确认消息?
【发布时间】:2016-09-08 00:44:49
【问题描述】:

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

= 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”

【问题讨论】:

  • 当用户更改电子邮件字段中的任何内容时,您必须使用 jquery/javascript,只需根据此更新消息

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


【解决方案1】:

使用 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();
    });
});

【讨论】:

  • 这只是给我一个错误“Uncaught SyntaxError: Unexpected identifier”
  • event.preventDefault();
  • 好的,试试 $(this.form).submit();而不是 event.preventDefault();
【解决方案2】:
$("#email_fields").change(function(){
    $('.email-button input').data('confirm', "Are these email addresses correct?  " + $("#email_fields").val())
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多