document.getElementById(“ID”)。值无效

时间:2013-02-25 13:36:54

标签: javascript html

我需要你的帮助。我有一个非常简单的问题,我无法弄清楚。很难解释,但我对一行有评论,我希望你能理解它。这是我的代码。对不起,但我似乎无法走出这个洞。

function CheckLength()
{
  var stringpassword = document.getElementById('Pass').value;
  alert(document.getElementById('Pass').value);   --> this part is only printing a blank. 
  /*    
  if(document.getElementById("Pass").value.length > 8)
  {
    return true;
  }
  else
  {
    alert(document.getElementById("Pass").value.length);
    return false;
  }
  */
}

function Check(a) 
{ 
  switch(a)
  {
    case 1 : 
      var first = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Fname").value.toUpperCase());
      var last = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Lname").value.toUpperCase());
      if(first !=-1 || last != -1)
      {
    alert("huy mali ang username. bawal yan pre");
    document.getElementById("Uname").value = "";
      }
      break; 

    case 2 :
      var gate = 0;
      var first = document.getElementById("Pass").value.toUpperCase().indexOf(document.getElementById("Fname").value.toUpperCase());
      var last = document.getElementById("Pass").value.toUpperCase().indexOf(document.getElementById("Lname").value.toUpperCase());

      if(first !=-1 || last != -1)
      {
        alert("mali dahil sa name");
        gate++;
      }
      if(!CorrectPassword())
      {     
        gate++;
      }
      if(!CheckLength())
      {    
        gate++;
      }

      if(gate != 0)
      {
    alert("Please follow password policy");
        document.getElementById("Pass").value="";
      }
      break;

    case 3:
      if(document.getElementById("Pass").value != document.getElementById("Rpass").value)
      {
    document.getElementById("Rpass").value = "";
      }
      break;
  }
}

身体某处:

<input type="Password" placeholder="Password" value="" id="Pass" onblur="Check(2)"/>

但是当我尝试的时候。在密码字段上输入后,然后点击其他一些。警报框仅返回空白警告框。即使是简单的赋值语句也不起作用?原谅我的英语不好

1 个答案:

答案 0 :(得分:1)

我让它工作了......我必须检查checkPassword(),因为你没有提供该功能。

在#Pass元素上,删除onblur属性。我将它分配给我提供的js的底部。

JSFiddle:http://jsfiddle.net/jeffshaver/vcRDp/1/

function CheckLength() {
    var stringpassword = document.getElementById('Pass').value;
    alert(document.getElementById('Pass').value);
/*  
    if(document.getElementById("Pass").value.length > 8)
    {
    return true;
    }
    else
    {
    alert(document.getElementById("Pass").value.length);
    return false;
    }
*/
}

function Check(a) { 
    switch(a) {
        case 1 : 
            var first = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Fname").value.toUpperCase());
            var last = document.getElementById("Uname").value.toUpperCase().indexOf(document.getElementById("Lname").value.toUpperCase());
            if(first !=-1 || last != -1) {
                alert("huy mali ang username. bawal yan pre");
                document.getElementById("Uname").value = "";
            }
            break; 

        case 2 :
            var gate = 0;
            if(first !=-1 || last != -1) {
                alert("mali dahil sa name");
                gate++;
            }
            if(!CheckLength()) {
                gate++;
            }

            if(gate != 0) {
                alert("Please follow password policy");
                document.getElementById("Pass").value="";
            }
            break;

        case 3:
            if(document.getElementById("Pass").value != document.getElementById("Rpass").value) {
                document.getElementById("Rpass").value = "";
           }
            break;
    }

}

documentgetElementById('Pass').addEventListener('blur', function() { Check(2) }, false);