好的,我在注册前使用jQuery进行用户名和电子邮件检查......
这会根据php的响应设置变量true或false。
$(document).ready(function() {
if (usr_checked == true) {
if (em_checked == true) {
$("#registerbttn").removeAttr("disabled");
}
}
else {
$("#registerbttn").attr("disabled", "disabled");
}
});
我究竟如何才能看到这些变量?
谢谢:D 下面的代码是设置函数的代码集之一
pic1 = new Image(16, 16);
pic1.src = "loader.gif";
$(document).ready(function(){
$("#username").change(function() {
var usr = $("#username").val();
if(usr.length >= 3)
{
$("#unstatus").html('<img src="loader.gif" align="absmiddle"> Checking availability...');
$.ajax({
type: "POST",
url: "check.php",
data: "username="+ usr,
success: function(msg){
$("#unstatus").ajaxComplete(function(event, request, settings){
if(msg == 'OK')
{
$("#username").removeClass('object_error'); // if necessary
$("#username").addClass("object_ok");
$(this).html(' <img src="accepted.png" align="absmiddle"> <font color="Green"> Available </font> ');
$("#registerbttn").removeAttr("disabled");
var usr_checked = true;
}
else
{
$("#username").removeClass('object_ok'); // if necessary
$("#username").addClass("object_error");
$("#registerbttn").attr("disabled", "disabled");
$(this).html(msg);
var usr_checked = false;
}
});
}
});
}
else
{
$("#status").html('<font color="red">The username should have at least <strong>3</strong> characters.</font>');
$("#username").removeClass('object_ok'); // if necessary
$("#username").addClass("object_error");
$("#registerbttn").attr("disabled", "disabled");
var usr_checked = false;
}
});
});
答案 0 :(得分:1)
我认为如果你不依赖变量并使用自定义事件会更好。例如,您可以创建并绑定到ajax检查程序完成时触发的自定义事件。这是一个示例代码,显示了它如何工作:
// on $(document).ready
$('#username')
// bind to a custom event triggered when validation of username succeeded
.bind('checkValid', function() {
alert('enabling the button');
$('#registerbttn').removeAttr('disabled');
})
// bind to a custom event triggered when validation of username failed
.bind('checkFailed', function() {
alert('disabling the button');
$('#registerbttn').attr('disabled', 'disabled');
});
// In your ajax handlers (i.e. $("#unstatus").ajaxComplete),
// if username was valid, trigger the valid event.
// This will call the checkValid handle we declared above and
// enable the button
$('#username').trigger('checkValid');
// If the username check failed though, trigger checkFailed instead
// which will call the handler above which disables the button
$('#username').trigger('checkFailed');
以下是其中的一个示例:http://jsfiddle.net/wU8vY/
答案 1 :(得分:0)
你没有得到它“看”变量。您必须在设置usr_checked的同一位置更改属性(我假设您在ajax调用返回时设置它)。
如果你发布你的ajax函数,我可能会更具体。