我不能把javascript if statment to activate my submit button if(username_ready && email_ready) ("#register").removeAttr("disabled");放在一起,所以我有两个变量,如果它们都是真的,我希望提交按钮是活动的。上面的方法似乎不起作用?我试着把它挂在这上面...
$(document).ready(function()
{
$("#username").blur(function()
{
//remove all the class add the messagebox classes and start fading
$("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn(1000);
//check the username exists or not from ajax
$.post("user_availability.php",{ username:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('This User name Already exists').addClass('messageboxerror').fadeTo(900,1);
var username_ready = false;
});
}
else
{
$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('Username available to register').addClass('messageboxok').fadeTo(900,1);
var username_ready = true;
});
}
});
});
});
</script>
<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script language="javascript">
$(document).ready(function()
{
$("#email").blur(function()
{
//remove all the class add the messagebox classes and start fading
$("#box").removeClass().addClass('messagebox').text('Checking...').fadeIn(1000);
//check the username exists or not from ajax
$.post("email_availability.php",{ email:$(this).val() } ,function(data)
{
if(data=='no') //if username not avaiable
{
$("#box").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('This email name Already exists have you forgot your password? ').addClass('messageboxerror').fadeTo(900,1);
var email_ready = false;
});
}
else
{
$("#box").fadeTo(200,0.1,function() //start fading the messagebox
{
//add message and change the class of the box and start fading
$(this).html('Tezac Hamra').addClass('messageboxok').fadeTo(900,1);
var email_ready = true;
});
}
});
});
});
发布于 2011-07-25 16:19:48
您在传递给.moveTo()函数的匿名函数中声明了username_ready和email_ready变量,这意味着您不能从这些特定函数外部访问它们。另外,这些变量是由两个独立的事件处理程序设置的,因此您需要以某种方式协调测试。
在我看来,最简单的方式如下所示(这是代码的一个过于简化的版本,所以您可能需要稍微修改一下):
$(document).ready(function() {
// declare the flags outside the other functions
var username_ready = false,
email_ready = false;
function checkSubmitStatus() {
if (username_ready && email_ready) {
$("#register").prop('disabled',false);
}
}
$("#email").blur(function() {
// put your existing code here, with just a change
// where you set email_ready to not declare it with `var`
// (it's now declared above)
email_ready = true; // or false
checkSubmitStatus();
}
$("#username").blur(function() {
// your other code here
username_ready = true; // or false
checkSubmitStatus();
}
});发布于 2011-07-25 16:03:55
disabled现在是一个属性,而不是一个属性。
$("#email, #username").blur(function(){
if(username_ready && email_ready)
{
$("#register").prop('disabled',false);
}
});https://stackoverflow.com/questions/6813157
复制相似问题