javascript document.getElementById("ID").value 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15068271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:22:24  来源:igfitidea点击:

document.getElementById("ID").value not working

javascripthtml

提问by user1548960

I need your help. I have a very simple problem that I cant figure out.Its hard to explain but I have a comment on a line and I hope you will understand it. here is my code. sorry for the long post but I cant seem to get out of this hole.

我需要你的帮助。我有一个很简单的问题,我想不通。它很难解释,但我在一行上有评论,希望你能理解。这是我的代码。很抱歉这篇长帖子,但我似乎无法摆脱这个漏洞。

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;
  }
}

somewhere in the body :

身体某处:

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

but when I try it. after typing on the password field.then clickng somehwere else. the alert box only returns a blank alert box. even the simple assignment statement is not working? forgive me for my bad english

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

回答by Jeff Shaver

I got it to work... I had to take out the check for checkPassword() because you didn't provide that function.

我让它工作了......我不得不取出 checkPassword() 的检查,因为你没有提供那个功能。

On your #Pass element, remove the onblur attribute. I assign it at the bottom of the js i provided.

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

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

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);