本文介绍了jQuery表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是:
当提交时:
- 对表单进行验证: OK
- 调用ajax以查看用户名和密码匹配:确定
- 如果匹配,则显示错误:确定
- ,然后重新提交表单: NOT OK 。
实际上麻烦的是,我无法提交表单,因为它上面有一个jquery提交事件!
函数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 {
//在这里完成!
}
});
返回false;
函数init(){
$('#form1')。submit(function(){
return form1Submit();
})
)
$(document).ready(function(){
init();
})
$您可以调用本地提交事件,所以请执行以下操作:
$( '#form1中')提交(form1Submit)。
然后在您的回调函数中执行此操作:
$。post(check.php,{username:username,password:password},function(data){
if(data ==ko){
alert('bad password');
} else {
this.submit();
}
});
this.submit()
isn' t叫他jQuery 触发器函数,而是本地< form>
功能。
My goal is:When for is submitted:
- 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.
Infact the trouble is, I cannot submit the form since there is a jquery submit event on it!
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();
})
解决方案
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.
这篇关于jQuery表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!