Javascript jQuery 表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3638750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery form submit
提问by yarek
My goal is: When for is submitted:
我的目标是:何时提交 for:
- a validation on form is made : OK
- an ajax is called to see that username and password do match : OK
- if they don't match, display an error: OK
- if they match, then REALLY SUBMIT the form: NOT OK.
- 对表单进行验证:好的
- 调用 ajax 以查看用户名和密码是否匹配:OK
- 如果它们不匹配,则显示错误:OK
- 如果它们匹配,那么真的提交表单:NOT OK。
Infact the trouble is, I cannot submit the form since there is a jquery submit event on it!
事实上问题是,我无法提交表单,因为上面有一个 jquery 提交事件!
function form1Submit() {
var username=$('#username').val();
var password=$('#password').val();
if (username.length<2) {
return false;
}
if (password.length<2) {
return false;
}
$.post("check.php", { username: username, password:password }, function(data) {
if (data=="ko") {
alert('bad password');
return false;
} else {
//to be done here !
}
});
return false;
}
function init() {
$('#form1').submit(function(){
return form1Submit();
})
}
$(document).ready(function(){
init();
})
采纳答案by Nick Craver
You can call the native submit event, so do this:
您可以调用本机提交事件,因此请执行以下操作:
$('#form1').submit(form1Submit);
Then in your post callback do this:
然后在你的帖子回调中这样做:
$.post("check.php", { username: username, password:password }, function(data) {
if (data=="ko") {
alert('bad password');
} else {
this.submit();
}
});
The this.submit()isn't calling he jQuery .submit()trigger function, but rather the native <form>.submit()function.
这this.submit()不是调用 jQuery.submit()触发器函数,而是调用本机<form>.submit()函数。
回答by A. M.
The problem is that form1Submit always returns false.
问题是 form1Submit 总是返回 false。
回答by andres descalzo
function form1Submit(ev, ok) {
ev.stopPropagation();
ok = (typeof ok != 'undefined') ? ok : false;
if (ok)
return true;
var username=$('#username').val(),
password=$('#password').val(),
selfForm = this;
if (username.length < 2)
return false;
if (password.length < 2)
return false;
$.post("check.php", { username: username, password:password }, function(data) {
if (data=="ko") {
alert('bad password');
} else {
$(selfForm).trigger('submit', [true]); // again submit but with ok parameter
}
});
return false;
}
function init() {
$('#form1').bind('submit', form1Submit);
}
$(document).ready(function(){
init();
})
回答by BalusC
The return falseis blocking the default form submit action. You have eitherto return truefrom the form1Submit()function to let the default form submit action do its job, orto add another $.post()inside the elsewhich submits the data to the form asynchronously, if your intent was to do it using ajaxical powers.
该return false阻塞默认的形式提交操作。你要么到return true从form1Submit()功能,让默认的形式提交行动完成自己的工作,或添加其他$.post()内else将数据提交到表单异步,如果你的目的是使用ajaxical权力这样做。

