简体   繁体   English

为什么功能“checkpassword”在点击时什么也不做?

[英]Why does function "checkpassword" makes nothing on click?

I have a problem guys.我有问题伙计们。 As a beginner, I am trying to check the length of password and return the result, but it doesn't work.作为初学者,我正在尝试检查密码的长度并返回结果,但它不起作用。 after clicking button, it does nothing.单击按钮后,它什么也不做。 What am I doing wrong?我究竟做错了什么?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script>
function checkpassword(){
    var pas=document.getElementByName(passcode).value;
    var x=pas.length;
    if(x<8){
        document.getElementById(message).innerHTML="Error 404!";
   }
   else{
    document.getElementById(message).innerHTML="It's acceptable"
   }
}
</script>
<body>
   
    <input type="password" name="passcode" placeholder="Enter password to check">
    <input type="submit" value="submit" onclick="checkpassword()">

<p id="message"></p>
</body>
</html>

@'Felix Kling' and @'Daniel A. White have given some hints in the comments because you will have to pass the names and ids as strings. @'Felix Kling' 和 @'Daniel A. White 在评论中给出了一些提示,因为您必须将名称和 ID 作为字符串传递。 But another issue is that .getElementByName() is not a function.但另一个问题是.getElementByName()不是函数。 You can try this (note that I have used .getElementsByName (plural) and referenced the zero-ith index:您可以试试这个(请注意,我使用了.getElementsByName (复数)并引用了第零索引:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script>
function checkpassword(){
    var pas=document.getElementsByName('passcode')[0].value;
    var x=pas.length;
    if(x<8){
        document.getElementById("message").innerHTML="Error 404!";
   }
   else{
    document.getElementById("message").innerHTML="It's acceptable"
   }
}
</script>
<body>
   
    <input type="password" name="passcode" placeholder="Enter password to check">
    <input type="submit" value="submit" onclick="checkpassword()">

<p id="message"></p>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM