我正在尝试使用Bootstrap 3模式窗体来触发jQuery中的form.submit()
,但无论我尝试什么,我都无法解决它。
<div class="modal fade" id="modal-signup" tabindex="-1" role="dialog" aria-labelledby="modal-signup" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Sign up</h4>
</div>
<div class="modal-body">
<form role="form" action="/" id="modal-signup-form" >
<div class="form-group">
<input type="email" class="form-control input-lg" placeholder="email">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="password">
</div>
<div class="form-group">
<input type="password" class="form-control input-lg" placeholder="confirm">
</div>
</form>
</div>
<div class="modal-footer">
<p>Already have account ? <a href="#" data-toggle="modal" data-target="#modal-signin">Sign in here.</a></p>
<input type="submit" class="btn btn-success btn-block btn-lg" value="Sign up">
</div>
</div>
</div>
</div>
和JS
$("#modal-signup-form").submit(function( event ) {
event.preventDefault();
alert("made it");
$.ajax({
type: "POST",
url: "adduser.php",
data: $('form.adduser_model').serialize(),
success: function (msg) {
$("#thanks").html(msg)
$("form.noteform").modal('hide');
},
error: function () {
alert("failure");
}
});
return false;
});
我创造了一个小提琴......
http://jsfiddle.net/menriquez/sreco3jt/
如果你点击了#34;注册&#34;和#34;注册&#34;在模型形式中,我希望事件能够触发并看到警报,但它并没有。
答案 0 :(得分:4)
这是因为提交按钮不在#modal-signup-form
表单元素中。
将按钮添加到<form>
的内部,或添加事件处理程序以在单击按钮时触发提交:
$(document).on('click', '#modal-signup-form-submit', function (e) {
$('#modal-signup-form').submit();
});
答案 1 :(得分:1)
您必须在关闭表单标记之前添加以下提交按钮代码。
<input type="submit" class="btn btn-success btn-block btn-lg" value="Sign up">
<强> JS Fiddle 强>