[英]return false not working on form submit in jquery
function SaleProduct() { var CusId=$('#CusId').val(); $.get('customer-id.php?CusId='+CusId, function(data) { if(data==0){ alert("Customer Id not valid.") return false; } }); }
<form action="sales-edit-insert.php" method="post" onSubmit="return SaleProduct()"> <input type="text" name="CusId" id="CusId"/> <!--Some input field here--> <input type="submit" value="Submit"/> </form>
return false; 返回false; or e.preventDefault();
或e.preventDefault(); not working on above function when I submit the form.
提交表单时无法使用上述功能。 Form is submitted after showing an alert.
显示警报后提交表单。
Your SaleProduct
returns nothing(actually undefined
). 您的
SaleProduct
返回任何内容(实际上是undefined
)。
You can stop immediate form sending with return false;
您可以停止立即发送表单,
return false;
inside of onsubmit
attribute: 在
onsubmit
属性内部:
<form action="sales-edit-insert.php" method="post" onSubmit="SaleProduct(); return false;">
And later you can submit your form manually: 之后,您可以手动提交表单:
function SaleProduct() {
var form = ...;
var CusId=$('#CusId').val();
$.get('customer-id.php?CusId='+CusId, function(data) {
if(data==0){
alert("Customer Id not valid.")
return;
}
form.submit();
});
return false; // you can also move this statement to here from attribute
}
Most simple way to get form element is provide it into onsubmit
: 获取表单元素的最简单方法是将其提供给
onsubmit
:
<form action="sales-edit-insert.php" method="post" onSubmit="return checkCustomer(this)">
And js: 和js:
function checkCustomer(form) {
//code from above
return false;
}
You're using jQuery already so why bother with the onsubmit
attribute. 您已经在使用jQuery,那么为什么还要麻烦
onsubmit
属性。 Try 尝试
<form action="sales-edit-insert.php" method="post" id="sale-product-form">
and 和
jQuery(function($) {
$('#sale-product-form').on('submit', function(e) {
e.preventDefault()
var form = this;
$.get('customer-id.php', $(form).serialize(), function(data) {
if (data == 0) {
alert('Customer Id not valid.')
} else {
form.submit() // submit normally
}
})
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.