Bootstrap 模式未在表单提交时关闭 - Rails [英] Bootstrap modal not closing on form submit - Rails
问题描述
我认为有这个模态:
............
因为表单是远程的,我希望这个模式在表单提交时隐藏.我是这样做的:
$('#addForm').submit(function() {$('#addListItem').modal('hide');});
现在发生的事情让我很困惑.当我点击提交时,表单远程提交并且模式保持打开状态.当我第二次点击提交按钮时,表单再次提交并且模式隐藏.为什么这个模式在第一次提交时没有隐藏?提前致谢!
我之前也遇到过类似的问题,我解决的方法是在提交按钮上添加一个点击事件:
$('#addSubmit').click ->$('#addListItem').modal('hide');
希望这会有所帮助!
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:
$('#addSubmit').click ->
$('#addListItem').modal('hide');
Hope this helps!
这篇关于Bootstrap 模式未在表单提交时关闭 - Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!