twitter-bootstrap Bootstrap 模式未在表单提交时关闭 - Rails

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23615549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:42:20  来源:igfitidea点击:

Bootstrap modal not closing on form submit - Rails

javascriptruby-on-railstwitter-bootstrap

提问by settheline

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!

目前正在发生的事情让我感到困惑。当我点击提交时,表单远程提交并且模式保持打开状态。当我第二次点击提交按钮时,表单再次提交并且模式隐藏。为什么这个模式在第一次提交时没有隐藏?提前致谢!

回答by miahabdu

I've had a similar problem before, and the way I solved this was by adding a click event to the submit button:

我以前也遇到过类似的问题,我解决这个问题的方法是向提交按钮添加一个点击事件:

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

Hope this helps!

希望这可以帮助!

回答by Michael Westbom

Try this:

试试这个:

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

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