2011-07-25 31 views
0

我不能放在一起javascript if statment to activate my submit button if(username_ready && email_ready) ("#register").removeAttr("disabled");所以我有两个增值税,如果他们都是真的我希望提交按钮被激活。以上似乎不工作?我试图把它拴住这个......删除禁用,如果如果语句真的在JavaScript

$(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; 
     }); 
     } 

    }); 

}); 
}); 

+0

你有一个巨大的多余的代码量。 我建议你创建一个函数来显示消息,因为所有正在改变的是“email”和“username”字样 关于来自AlienWebguy的消息,请阅读[prop](http://api.jquery.com/prop /) – mplungjan

回答

1

您声明您传递给.moveTo()功能,这意味着你不能从那些特定功能的外部访问他们的匿名函数中你username_readyemail_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(); 
    } 

}); 
3

disabled是一个属性,而不是现在的属性。

$("#email, #username").blur(function(){ 
    if(username_ready && email_ready) 
    { 
     $("#register").prop('disabled',false); 
    } 
}); 
+0

好吧,所以当我把它放在它阻碍ajax正常工作你会介意哪里把if语句放在两个ajax节中的任何一个中,还是应该使它成为它自己的函数?我在考虑把它放在真实陈述的其他部分。这样它就在变量设置为true后立即运行。谢谢。 – Osman

+0

我试过'data == no'条件后:'if(data =='no'){} else {} if(username_ready ...' – AlienWebguy

+0

谢谢,如果我在阅读之前就知道了你会为我节省一些时间!但是你是对的! – Osman