我有以下 Twitter Bootstrap 3 模态,模态内有一个表单:
<!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h2 class="modal-title">Add new page</h2>
</div>
<div class="modal-body">
<form class="form-horizontal add-new-page-form">
<fieldset>
<!-- Label -->
<div class="form-group">
<label class="col-md-4 control-label" for="labelInput">Label</label>
<div class="col-md-5">
<input id="labelInput" name="labelInput" type="text" data-field="label" class="form-control input-md">
<span class="help-block">Page label</span>
</div>
</div>
<!-- Button (Double) -->
<div class="form-group">
<label class="col-md-4 control-label" for="closeBtn"></label>
<div class="col-md-8">
<button id="closeBtn" name="closeBtn" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" id="saveBtn" name="saveBtn" class="btn btn-primary add-page" value="Add">
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</div>
我想通过 jQuery 捕获 submit
事件:
$("form").on("submit", function () {
alert("foo");
return false;
});
表单不是通过按 Enter 键提交的(当文本输入获得焦点时),而是在 Add 按钮(即 submit
一)被点击。
我找不到为什么不通过 Enter 提交表单。模式只是关闭,但 submit
事件没有发出/触发。
JSFDDLE - 在哪里可以重现问题
在 Chromium 版本 34.0.1847.116 Ubuntu 14.04 aura (260972) 和 Firefox 31.0 上测试。两种浏览器都有同样的问题。
最佳答案
你可以简单地改变顺序
close-button
submit-button
到
submit-button
close-button
或添加
type="button"
到您的关闭按钮,这样它就不会被解释为提交按钮
干杯
关于jquery - 按下回车键时 Bootstrap 3 模式关闭,但未提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24958558/