【发布时间】:2015-10-27 09:02:19
【问题描述】:
我在两个不同的 Bootstrap 模式中嵌入了两种不同的表单。它们被正确调用并正确加载。我可以看到每个表单(在页面的源代码中)都分配了正确的操作。做了一些侦探工作,我发现无论我有多少带有表单的模态,它们都提交给相同的动作/表单,这是我的代码中首先出现的那个。
很明显,我没有正确关闭某些东西或有重复的 ID 或其他东西,但我不知道是什么导致了问题。这里有两种形式。如果我只加载一个或另一个,每个都可以正常工作。如果我同时加载两者,那么两个表单都会在第一个表单中提交操作。
值得注意的是,我有另一种形式,它不在模态中,它总是可以正常工作,并且在我的代码中出现在这两种其他形式之前。
<div class="modal fade" id="get_started-complete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style=" overflow: scroll; height:auto;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Getting Started</h4>
</div>
<div class="modal-body">
some text here
<form class="form form-quote dark" id="form_1" action="get_started_1.php" method="post" style="margin-top:5px;">
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="fullname_1" class="col-sm-3 col-xs-12 control-label">Full Name</label>
<div class="col-sm-9 col-xs-12">
<input type="text" style=" margin-top: 5px;" class="form-control" name="fullname_1" id="fullname_1" placeholder="">
</div>
</div>
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="company_name_1" class="col-sm-3 col-xs-12 control-label">Company Name</label>
<div class="col-sm-9 col-xs-12">
<input type="text" style=" margin-top: 5px;" class="form-control" name="company_name_1" id="company_name_1" placeholder="">
</div>
</div>
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="email_1" class="col-sm-3 col-xs-12 control-label">Email Address</label>
<div class="col-sm-9 col-xs-12">
<input type="email" style=" margin-top: 5px;" class="form-control required email" name="email_1" id="email_1" placeholder="">
</div>
</div>
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="database_size_1" class="col-sm-3 col-xs-12 control-label">Database Size</label>
<div class="col-sm-9 col-xs-12">
<input type="text" style=" margin-top: 5px;" class="form-control" name="database_size_1" id="database_size_1" placeholder="Enter a number">
</div>
</div>
<div class="form-group">
<div id="recap4"></div>
</div>
<input type="text" name="form_location" id="form_location" value="complete" hidden>
<button type="submit" id="get_started_submitted-c" class="btn btn-primary btn-lg btn-block">Submit Form</button>
(Click once and give it a second or two...)
</form>
</div>
</div>
</div>
</div>
<!--FORM 2-->
<div class="modal fade" id="get_started-advanced" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style=" overflow: scroll; height:auto;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel">Getting Started</h4>
</div>
<div class="modal-body">
some text here
<form class="form form-quote dark" id="form_2" action="get_started_2.php" method="post" style="margin-top:5px;">
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="fullname_2" class="col-sm-3 col-xs-12 control-label">Full Name</label>
<div class="col-sm-9 col-xs-12">
<input type="text" style=" margin-top: 5px;" class="form-control" name="fullname_2" id="fullname_2" placeholder="">
</div>
</div>
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="company_name_2" class="col-sm-3 col-xs-12 control-label">Company Name</label>
<div class="col-sm-9 col-xs-12">
<input type="text" style=" margin-top: 5px;" class="form-control" name="company_name_2" id="company_name_2" placeholder="">
</div>
</div>
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="email_2" class="col-sm-3 col-xs-12 control-label">Email Address</label>
<div class="col-sm-9 col-xs-12">
<input type="email" style=" margin-top: 5px;" class="form-control required email" name="email_2" id="email_2" placeholder="">
</div>
</div>
<div class="form-group" style="line-height:0px;">
<label style="line-height:25px;" for="database_size_2" class="col-sm-3 col-xs-12 control-label">Database Size</label>
<div class="col-sm-9 col-xs-12">
<input type="text" style=" margin-top: 5px;" class="form-control" name="database_size_2" id="database_size_2" placeholder="Enter a number">
</div>
</div>
<div class="form-group">
<div id="recap3"></div>
</div>
<input type="text" name="form_location_2" id="form_location_2" value="advanced" hidden>
<button type="submit" id="get_started_submitted-a" class="btn btn-primary btn-lg btn-block">Submit Form</button>
(Click once and give it a second or two...)
</form>
</div>
</div>
</div>
</div>
我的提交处理程序导致问题的理论。看起来是这样的:
submitHandler: function(form) {
var $this = $(form);
$.ajax({
url: $this.attr('action'),
type: 'POST',
data: $this.serialize(),
})
.done(function(msg) {
if( msg == 'ok' ) {
toastr.success('Thank you. We will be in touch within 24 hours.');
$this[0].reset();
} else {
toastr.error('Sorry. An error occured. Please try again later or email us directly ');
}
})
.fail(function() {
toastr.error('An error occured. Please try again later or email us directly .');
});
}
【问题讨论】:
-
是否有通过 ajax 提交的提交处理程序?还是这些默认浏览器提交?
-
@charlietfl 我真的不确定。我该如何检查?
-
是否重新加载页面或者您是否在浏览器开发工具网络中看到了请求
-
@charlietfl 页面没有重新加载。我在开发工具中看到了请求。
-
问题在于提交处理javascript。我的猜测是它从第一个表单
action获取 url
标签: javascript php jquery forms twitter-bootstrap