【问题标题】:jQuery on submit function not working on form (with HAML)提交功能上的 jQuery 在表单上不起作用(使用 HAML)
【发布时间】:2014-02-11 10:24:47
【问题描述】:

我有下面的 jQuery 代码来检测提交事件并触发一些其他的 javascript 代码:

$('form#send-message').on('submit', function(){
      console.log('test');
      $('body').modalmanager('loading');
    });

但这不适用于:

.modal-header
  Contact info
  %button.close{ :type => 'button', 'data-dismiss' => 'modal', 'aria-hidden' => 'true' }
    ×
.modal-body
  = raw @booth.contact_info
  %hr
  = simple_form_for(@booth, url: booth_send_message_path, method: :post, html: { id: "send-message" }, remote: true) do |f|
    .form-group
      %textarea#message.form-control{name: "user[message]", placeholder: "Type your message here..."}
    %input{type: "hidden", name: "user[id]", value: "#{current_user.id}"}
    .form-actions
      %button.btn.btn-warning{:type => 'submit'} Send
      = f.error_notification

表单的 id 肯定是匹配的,但似乎什么都没有触发

事件无法正常工作是否有原因?

【问题讨论】:

    标签: javascript jquery ruby-on-rails haml


    【解决方案1】:
    input type=submit and button type=submit 
    

    是不同的东西。所以只需将按钮更改为输入或将回调绑定到按钮单击事件

    %input.btn.btn-warning{:type => 'submit'} Send
    

    【讨论】:

    • %button.btn.js_submit_form.btn-warning{:type => 'submit'} 发送, $(".js_submit_form").on("click", function(){ //逻辑返回 false;});
    【解决方案2】:

    在haml文件中试试这个:

    .form-actions
      = f.button :submit, 'Send', class: 'button btn btn-warning'
    

    【讨论】:

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