gpt4 book ai didi

javascript - 模式中的表单以相同的操作提交(相同的 PHP 文件)

转载 作者:行者123 更新时间:2023-11-30 00:24:46 24 4
gpt4 key购买 nike

我在两个不同的 Bootstrap 模态中嵌入了两种不同的形式。它们被正确调用并正确加载。我可以看到每个表单(在页面的源代码中)都分配了正确的操作。做了一些侦探工作,我发现无论我有多少个带有表单的模态,它们都提交给相同的 Action /表单,这是我的代码中排在第一位的那个。

很明显,我没有正确关闭某些东西或有重复的 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">&times;</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">&times;</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 .');
});
}

最佳答案

在一个选择器中调用多个表单时,似乎 validate() 插件无法正常工作。

尝试以这种方式初始化:

$('form.form-quote').each(function(){
$(this).validate({ /* options */});

});

应该更好地隔离实例

参见 https://stackoverflow.com/a/19209548/1175966

关于javascript - 模式中的表单以相同的操作提交(相同的 PHP 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31818065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com