首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在javascript中删除disable if语句true

在javascript中删除disable if语句true
EN

Stack Overflow用户
提问于 2011-07-25 15:56:36
回答 2查看 471关注 0票数 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;  
        });
      }

    });

});
});

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-25 16:19:48

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

在我看来,最简单的方式如下所示(这是代码的一个过于简化的版本,所以您可能需要稍微修改一下):

代码语言:javascript
复制
$(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();
  }

});
票数 1
EN

Stack Overflow用户

发布于 2011-07-25 16:03:55

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

代码语言:javascript
复制
$("#email, #username").blur(function(){
    if(username_ready && email_ready)
    {
       $("#register").prop('disabled',false);
    }
});
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6813157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档