我在提交时调用了一个函数来检查是否完成了所需的上传,如果数据库中没有上传,则不应该提交表单。
通过使用alert,我可以看到我的checkuploads.asp正在返回正确的值,并且它会显示未找到数据的警报,但不会阻止表单提交。
$(window).load(function() {
$('#Save').click(function() {
$.ajax({
type: "POST",
url: "checkuploads.asp?value=" + $('#RequestNo').val(),
success: function(data) {
if (data === "Not Found") {
alert("the data was not found");
data.preventDefault();
window.history.back();
return false;
} else
if (data === "Found") {
alert("the data was found");
alert(data);
}
}
});
});
});提前谢谢。
发布于 2018-03-01 13:24:26
因为您使用的是ajax请求,所以在提交表单之前,响应将会花费一些时间。所以,你需要在ajax调用之后执行return false;。
$(window).load(function() {
$('#Save').click(function() {
$.ajax({
type: "POST",
url: "checkuploads.asp?value=" + $('#RequestNo').val(),
success: function(data) {
if (data === "Not Found") {
alert("the data was not found");
//data.preventDefault();//Remove this un-necessary line
window.history.back();
//return false;//Remove this too. It is un-necessary
} else
if (data === "Found") {
alert("the data was found");
alert(data);
//Replace the form id with correct one
("#myForm").submit();//Add this line
}
}
});
return false;
});
});下面是完整的示例:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta charset="UTF-8">
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
</head>
<body>
<div>
<form id="myForm" action="/my-listener">
<input type="text" name="RequestNo" id="RequestNo" />
<input type="button" id="Save" value="Save" />
</form>
</div>
<script>
$(document).ready(function() {
$('#Save').click(function() {
$.ajax({
type: "POST",
url: "checkuploads.asp?value=" + $('#RequestNo').val(),
success: function(data) {
if (data === "Not Found") {
alert("the data was not found");
window.history.back();
} else if (data === "Found") {
alert("the data was found");
alert(data);
$("#myForm").submit();
}
}
});
return false;
});
});
</script>
</body>
</html>发布于 2018-03-01 13:24:40
试试这个:
$(window).load(function() {
$('#Save').click(function(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "checkuploads.asp?value=" + $('#RequestNo').val(),
success: function(data) {
if (data === "Not Found") {
alert("the data was not found");
data.preventDefault();
window.history.back();
return false;
} else
if (data === "Found") {
alert("the data was found");
window.location.replace( "checkuploads.asp?value=" + $('#RequestNo').val()); // you can chanbge url as per your need
// in case of form use
$('#form').submit();
alert(data);
}
}
});
return false;
});
});https://stackoverflow.com/questions/49043023
复制相似问题