【发布时间】:2017-03-13 14:53:18
【问题描述】:
我有这个问题,我的表单双重发布数据(通过单击在表中创建了 2 个条目)。我遵循了有关 stackoverflow 的建议,但似乎没有任何效果。可能是什么问题?我是一个使用 AJAX 的菜鸟,所以我必须假设我在做一些愚蠢的事情
这个脚本的基本逻辑是,提交和保存数据,如果成功返回成功则重定向,否则在DIV中显示输出和错误信息。
我尝试过的建议:
How can I prevent a double submit($.post) with jQuery
jquery ajax form submits twice
Prevent double submission of forms in jQuery
到目前为止我的代码:
$(document).ready(function() {
$("#go").click(function(e) {
e.preventDefault()
e.stopImmediatePropagation();
$.ajax({
type: "POST",
url: "controllers/newcases_controller.php",
data: $('#casedata').serialize(),
success: function(response) {
if (response == 'success')
window.location.replace("listcases.php");
else
/* clear old results, then display the new results */
$("#divResults").empty().append(response);
}
});
return false;
});
});
这是表单的一部分,但我假设您只希望看到按钮是如何定义的
<form role="form" class="form-horizontal" id="casedata">
<div class="form-group">
<label class="col-sm-12" for="TextArea">Additional Address Details [ Max 200 Characters ] <span id="countdown2"></span></label>
<div class="col-sm-12"><textarea class="form-control" id="additionaladdressdetails" name="additionaladdressdetails" placeholder="e.g Unit 123, Complex Street Name"></textarea></div>
</div>
<div class="form-group">
<div class="col-sm-12">
<button type="reset" class="btn btn-default pull-right">Cancel</button>
<button type="button" id="go" name="go" class="btn btn-primary pull-right">Create Case</button>
</div>
</div>
</form>
建议下一步尝试?
【问题讨论】:
-
能否请您提供您正在使用的html
-
#go类型不是submit我假设? -
当您在 html 中包含两次 js 文件时也会发生这种情况。
标签: jquery ajax post http-post double