我有两种不同的形式嵌入在两个不同的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 .');
});
}
答案 0 :(得分:1)
似乎在一个选择器中调用多个表单时,validate()插件不能很好地工作。
尝试以这种方式初始化:
getActivity().getSupportFragmentManager()
应该更好地隔离实例