如果if语句在javascript中为true,则删除disable

时间:2011-07-25 07:56:36

标签: javascript

我无法将javascript if statment to activate my submit button if(username_ready && email_ready) ("#register").removeAttr("disabled");放在一起,所以我有两个vars,如果它们都是true,我希望提交按钮处于活动状态。以上似乎不起作用?我试着把它拖进这个......

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

    });

});
});

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

您在传递给username_ready函数的匿名函数中声明了email_ready.moveTo()变量,这意味着您无法从这些特定函数外部访问它们。这些变量也是由两个独立的事件处理程序设置的,所以你需要以某种方式协调测试。

对我来说最简单的方法就像下面这样(这是你的代码的过度简化版本,所以你可能需要稍微改编一下):

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

});