jquery 表单验证器从不进行 ajax 调用
这部分是此线程的延续: jquery - 从回调函数(在 post 请求中)返回值到其内部的函数? 因为我更新了代码,但问题仍然存在。我正在使用 jquery 验证一个简单的 html 表单,尽管我所有其他 if/else 语句都有效,但 ajax 调用永远不会进行。这是 JavaScript 代码:
var pass_form = $('#pass_form'); pass_form.submit( valid_pass_sett );
function valid_pass_sett() {
//remove old errors - snipped
pass_old = $('input[name=pass_old]').val();
pass_new = $('input[name=pass_new]').val();
pass_confirm_new = $('input[name=pass_confirm_new]').val();
if (pass_old === "") {
//display error on form - snipped
return false;
} else if (pass_new === "") {
//display error on form - snipped
return false;
} else if (pass_new != pass_confirm_new) {
//display error on form - snipped
return false;
} else if (pass_new.length < 8) {
//display error on form - snipped
return false;
} else {
$.post("http://www.example.com/ajax/validate.php",{ // async validation
type: 'valid_old_change_pass',
pass_old: pass_old,
pass_new: pass_new
}, valid_pass_combo_callback);
alert('after the ajax call...');
}
return false; // cancel form submission
}
这是 validate.php 的相关部分:
$username = $_SESSION['username'];
$pass_old = $_POST['pass_old'];
$pass_new = $_POST['pass_new'];
if (empty($pass_old) || empty($pass_new)) {
echo "invalid";
} else if (!User::valid_user_pass($username, $pass_old)) {
echo "invalid_old";
} else if (!Sanitize::is_legal_password($pass_new)) {
echo "invalid_new";
} else {
echo "valid";
}
当我使用 Firebug 进行调试并且所有其他表单输入都正确时,脚本会进行 ajax 调用,然后提交表单,即使它应该调用回调函数。这是回调函数的代码:
function valid_pass_combo_callback( data ) {
if (data == 'valid') {
//only if the form is valid!
pass_form[0].submit();
}
else if (data == "invalid_old") {
//display error on form - snipped
}
else if (data == "invalid_new") {
//display error on form - snipped
}
else {
//it always jumps to here..., even though data *is* the correct value
}
}
编辑 redux:好的,我修复了回调函数中的错误,如第一个答案中所示,现在出现了一些不同的问题。我调试了函数 valid_pass_combo_callback
并且它从 validate.php 获取了正确的值;在本例中,invalid_old
是返回的值。当我调试时,data
等于invalid_old
。但是,比较失败...因此无论如何,代码总是跳转到最后一个else语句。什么也没有发生,因为那里没有任何行为,那么为什么比较总是失败呢?
编辑,已解决:我决定放弃绑定此函数来提交,而是绑定到表单上按钮的 onclick 事件(我用它来代替提交按钮),这解决了问题。单击按钮时调用验证,如果客户端验证通过,则将表单提交到服务器进行验证。
This is partly a continuation from this thread: jquery - return value from callback function (in post request) into the function its inside of?
because I updated the code, yet trouble persists. I'm validating a simple html form with jquery, and despite all my other if/else statements working, the ajax call never gets made. Here's the javascript code:
var pass_form = $('#pass_form');
pass_form.submit( valid_pass_sett );
function valid_pass_sett() {
//remove old errors - snipped
pass_old = $('input[name=pass_old]').val();
pass_new = $('input[name=pass_new]').val();
pass_confirm_new = $('input[name=pass_confirm_new]').val();
if (pass_old === "") {
//display error on form - snipped
return false;
} else if (pass_new === "") {
//display error on form - snipped
return false;
} else if (pass_new != pass_confirm_new) {
//display error on form - snipped
return false;
} else if (pass_new.length < 8) {
//display error on form - snipped
return false;
} else {
$.post("http://www.example.com/ajax/validate.php",{ // async validation
type: 'valid_old_change_pass',
pass_old: pass_old,
pass_new: pass_new
}, valid_pass_combo_callback);
alert('after the ajax call...');
}
return false; // cancel form submission
}
and here's the relevant part of the validate.php:
$username = $_SESSION['username'];
$pass_old = $_POST['pass_old'];
$pass_new = $_POST['pass_new'];
if (empty($pass_old) || empty($pass_new)) {
echo "invalid";
} else if (!User::valid_user_pass($username, $pass_old)) {
echo "invalid_old";
} else if (!Sanitize::is_legal_password($pass_new)) {
echo "invalid_new";
} else {
echo "valid";
}
When I'm debugging with Firebug, and all other form inputs are correct, the script gets to the ajax call, then submits the form, even though it's supposed to call the callback function. This is the code for the callback function:
function valid_pass_combo_callback( data ) {
if (data == 'valid') {
//only if the form is valid!
pass_form[0].submit();
}
else if (data == "invalid_old") {
//display error on form - snipped
}
else if (data == "invalid_new") {
//display error on form - snipped
}
else {
//it always jumps to here..., even though data *is* the correct value
}
}
EDIT redux: Ok, I fixed the error in my callback function, as seen in the first answer, and now, a bit of a different problem has emerged. I debugged the function valid_pass_combo_callback
and it's getting the correct value from validate.php; in this case, invalid_old
is the value being returned. When I debug, data
is equal to invalid_old
. However, the comparison fails... so the code always jumps to the last else statement, no matter what. Nothing happens, because there isn't any behaviour there, so why is the comparison always failing?
EDIT, SOLVED: I decided to forgo binding this function to submit, and instead bound to an onclick event for a button on the form (which I'm using in place of a submit button) and that solved the problem. Validation is called when the button is clicked, and if client-side validation passes, then the form is submitted to the server for validation there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的一个问题是您正在调用回调,而不是传递函数本身:
One problem here is you're invoking your callback, not passing the function itself:
查看您的代码,您不必提交表单,而是进行 ajax 调用,但您说表单正在提交。我认为下面的代码中有一些 js 错误,因此表单被提交。
为什么不阻止表单提交的默认行为。
并在回调方法中进行一些小更改以取消绑定提交处理程序,然后提交表单。这是因为我们已经有一个提交处理程序,并且我们不想下次在下面的方法中提交时调用它。
Looking at your code you dont have to submit the form but make an ajax call but you say the form is getting submitted. I think there is some js error in the below peice of code because of which the form is getting submitted.
Why dont you prevent the default behavior of form submit.
And in the callback method make a small change to unbind the submit handler and then submit the form. This is because we already have one submit handler and we dont want to call it next time when we submit in the below method.