我对jQuery和javascript编程非常新颖。我有一个下面的程序检查用户名是否被占用。目前,PHP脚本始终返回。jQuery.validator.addMethod中的.post总是返回false
if(isset($_POST["username"]))//&& isset($_POST["checking"]))
{
$xml="<register><message>Available</message></register>";
echo $xml;
}
登录函数可以工作,但用户名检查不会。有任何想法吗? 这里是我的所有代码:
$(document).ready(function() {
jQuery.validator.addMethod("checkAvailability",function(value,element){
$.post("login.php" , {username:"test", checking:"yes"}, function(xml){
if($("message", xml).text() == "Available") return true;
else return false;
});
},"Sorry, this user name is not available");
$("#loginForm").validate({
rules: {
username: {
required: true,
minlength: 4,
checkAvailability: true
},
password:{
required: true,
minlength: 5
}
},
messages: {
username:{
required: "You need to enter a username." ,
minlength: jQuery.format("Your username should be at least {0} characters long.")
}
},
highlight: function(element, errorClass) {
$(element).fadeOut("fast",function() {
$(element).fadeIn("slow");
})
},
success: function(x){
x.text("OK!")
},
submitHandler: function(form){send()}
});
function send(){
$("#message").hide("fast");
$.post("login.php" , {username:$("#username").val(), password:$("#password").val()}, function(xml){
$("#message").html($("message", xml).text());
if($("message", xml).text() == "You are successfully logged in.")
{
$("#message").css({ "color": "green" });
$("#message").fadeIn("slow", function(){location.reload(true);});
}
else
{
$("#message").css({ "color": "red" });
$("#message").fadeIn("slow");
}
});
}
$("#newUser").click(function(){
return false;
});
});
对不起:(它没有工作,返回false我已经改变了成功的回调函数为:如果($( “信息”,XML)的.text()== “Available”)return true; else return false; – 2010-06-06 08:09:55
@abdullah - 这个'return'函数没有什么区别,只是额外的代码......如果你发布的答案有效,但是这个并不是那个php文件你的问题是不准确的,你应该更新它 – 2010-06-06 09:57:43
@Nick:我认为问题是虚拟函数返回“checkAvailability”之后的参数,它是函数(值,元素){} 。实际上,它返回什么〜=假, 对?它只是做了一些ajax函数,它返回了一些值。但是“函数(值,元素)”本身并没有返回任何东西。我是一个初学者,只是我的想法虽然:) – 2010-06-06 21:41:28