简体   繁体   中英

HTML and Javascript. Why does my code not work

This is a quite simple piece of HTML with a bit of JavaScript integrated. I did what I could from what I know, but I don't think I used the correct document tags. Its either that or the page needs a way of updating, which I don't know how to do. Please help.

<!DOCTYPE html>
<html>
<body>
    <form action="">
        User name:<br>
        <input type="text" name="userid">
        <br>
        User password:<br>
        <input type="password" name="psw">
    </form>

    <p>The characters in a password field are masked (shown as asterisks or circles).</p>

    <p id="enter"></p>

    <script>
        function access(username, password) {
            if (username == "bobby") {
                if (password == "qwertyasd123") {
                    document.getElementById(enter).innerHTML = "You may pass";
                }
            }
        }
        access(document.getElementsByName("userid").innerHTML, 
            document.getElementsByName("userid").innerHTML)
    </script>
</body>
</html>

Change

document.getElementById(enter)

to

document.getElementById('enter')

Also change:

document.getElementsByName("userid").innerHTML

to

document.getElementsByName("userid")[0].value

And you didn't change userid to psw for one your strings.

there where quite some mistakes indeed:

of document.getElementsByName("userid") you get an list, so you want to access the first by [0] :

document.getElementsByName("userid")

You forgot to use the id as a string here: document.getElementById(enter) should be document.getElementById("enter")

You used "userid" twice and not "psw" aswell:

access(document.getElementsByName("userid")[0].value, 
            document.getElementsByName("psw")[0].value)

You want it to happen when you press enter:

<form onsubmit='access(document.getElementsByName("userid")[0].value, document.getElementsByName("psw")[0].value)'>

and you will need a <input type="submit" name="sub"> for that aswell.

Here is the complete code and the working example : https://jsfiddle.net/j35fbet9/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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