简体   繁体   English

Bootstrap模式没有关闭表单提交 - Rails

[英]Bootstrap modal not closing on form submit - Rails

I have this modal in my view: 我认为我有这种模式:

<div class="modal fade" id="addListItem" tableindex="-1" role="dialog" aria-labelledby="addListItem" aria-hidden="true">
  ......
  ......
      <div class="modal-body">
         <%= form_for @list_item, :remote => true, :html => {:id => "addForm"} do |f| %>
             <%= f.hidden_field :upc, :value => @item.upc %>
             <%= f.hidden_field :inventory_item_id, :id => "inventoryID", :value => "" %>
             <%= f.submit("Add It!", class: "btn", :id => "addSubmit") %>
         <% end %>
      </div>
  ......
  ......
</div>

Because the form is remote, I want this modal to hide on form submit. 因为表单是远程的,我希望这个模式隐藏在表单提交上。 Here's how I do that: 我是这样做的:

$('#addForm').submit(function() {
    $('#addListItem').modal('hide');
});

What's currently happening is puzzling me. 目前正在发生的事情令我感到困惑。 When I hit submit, the form submits remotely and the modal stays open. 当我点击提交时,表单会远程提交并且模态保持打开状态。 When I hit the submit button a second time, the form submits again and the modal hides. 当我第二次点击提交按钮时,表单再次提交,模式隐藏。 Why is this modal not hiding on the first submit? 为什么这个模态不会隐藏在第一次提交中? Thanks in advance! 提前致谢!

I've had a similar problem before, and the way I solved this was by adding a click event to the submit button: 我之前遇到过类似的问题,我解决这个问题的方法是在提交按钮中添加一个click事件:

$('#addSubmit').click ->
   $('#addListItem').modal('hide');

Hope this helps! 希望这可以帮助!

尝试这个:

$(modal).on("click", 'input[type="submit"]', (e) -> modal.modal('hide')

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

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