I have the following Twitter Bootstrap 3 modal, with a form inside of the modal:
<!-- 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>
I want to catch the submit
event via jQuery:
$("form").on("submit", function () {
alert("foo");
return false;
});
The form is not submitted via pressing Enter key (when an text input is focused), but it is submitted when Add button (that is a submit
one) is clicked.
I cannot find why the form is not submitted via Enter. The modal simply closes, but submit
event is not emitted/fired.
JSFDDLE - where the issue can be reproduced
Tested on Chromium Version 34.0.1847.116 Ubuntu 14.04 aura (260972) and Firefox 31.0. Same issue on both browsers.
When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.
The goal is to submit the data to our db when the submit button is clicked and then hide the form and display a thank you within the same modal. Here's the basics of the code for the modal: <div class="modal fade" id="formModal" tabindex="-1" role="dialog" aria-labelledby="formModalLabel" aria-hidden="true">
Show activity on this post. and write your html code in update panel then remove data-dismiss="modal" from the button. Hope it works for you.
Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.
you can simply change the order from
close-button
submit-button
to
submit-button
close-button
or add
type="button"
to your close button so it doesn´t get interpreted as submit button
cheers
I've added a handler to the modal close event to submit the form before closing the modal, like so:
$("#myModal").on("hide.bs.modal", function () {
$(this).find('form').submit();
});
I've updated the jsfiddle correspondingly, http://jsfiddle.net/yAzrs/2/
You could also listen for the enter key within the form itself, something like this:
$('form').on("keypress", function (e) {
if (e.which == 13) $(this).submit();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With