admin管理员组

文章数量:1279084

I have a function with input var is name of Form and ID of input. Now I want to get the value of password input. The problem is I have some input with same ID but in different form. So I want a flexible way to get its value when I know its form.

I have tried var x = Form_Name.ID_Name.value;

but it doesn't work. So please tell me how to get the value of password input by a flexible way. Thanks so much. Below is my code:

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check(Form_Name, ID_Name)
        {
            alert(document.getElementById(ID_Name).value);
            alert(Form_Name);
            var x = Form_Name.ID_Name.value;
            alert(x);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check('MainForm','ID')"> Click Me </button>
    </div>
</body>
</html>

I have a function with input var is name of Form and ID of input. Now I want to get the value of password input. The problem is I have some input with same ID but in different form. So I want a flexible way to get its value when I know its form.

I have tried var x = Form_Name.ID_Name.value;

but it doesn't work. So please tell me how to get the value of password input by a flexible way. Thanks so much. Below is my code:

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check(Form_Name, ID_Name)
        {
            alert(document.getElementById(ID_Name).value);
            alert(Form_Name);
            var x = Form_Name.ID_Name.value;
            alert(x);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check('MainForm','ID')"> Click Me </button>
    </div>
</body>
</html>
Share Improve this question edited Dec 24, 2015 at 8:29 nptn asked Dec 23, 2015 at 8:47 nptnnptn 771 gold badge3 silver badges10 bronze badges 6
  • 5 To get password value, you can use document.getElementById('Pass').value – Tushar Commented Dec 23, 2015 at 8:49
  • 1 Use regular DOM methods like document.getElementById(), document.getElementsByTagName(), etc. Exposing every item as a global variable is something you can expect in Internet Explorer 4 but not in modern browsers. – Álvaro González Commented Dec 23, 2015 at 8:51
  • @ÁlvaroGonzález where do you see global variables ? Form_Name is a string .. also console.log(Pass.value); work in latest chrome – Hacketo Commented Dec 23, 2015 at 9:00
  • @Hacketo I thought the intention of Form_Name.ID_Name.value might be that even though the call passes a string. – Álvaro González Commented Dec 23, 2015 at 9:31
  • @ÁlvaroGonzález I guess you're right about the intention, but this way work in modern browsers (at least on my chrome and firefox). this work console.log(window[Form_Name][ID_Name].value); – Hacketo Commented Dec 23, 2015 at 9:35
 |  Show 1 more ment

2 Answers 2

Reset to default 4

I have created another button for you to get password.

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check(Form_Name, ID_Name)
        {
            alert(document.getElementById(ID_Name).value);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check('MainForm','ID')"> Click Me for ID </button>
        <button value="click Me" onclick="check('MainForm','Pass')"> Click Me for Password</button>
    </div>
</body>
</html>

Optimized way: You can achieve it using the same button as well, by passing an array of ID's.

Let me know, I can help more on this.

Access username and password using it's id.

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="lolkittens" />
    <script charset="utf-8" type="text/javascript">
        function check()
        {
            var username = document.getElementById("ID").value;
            var password = document.getElementById("Pass").value;
            alert("Username: "+username+"  Password: "+password);
        }
    </script>
    <title>Untitled 1</title>
</head>

<body>

    <div id="main" style="width: 300px; height: 200px;">
        <form name="MainForm" id="MainForm">
            <input id="ID" name="ID" type="text" size="20"/>
            <input id="Pass" name="Pass" type="password" size="20"/>
        </form>
        <button value="click Me" onclick="check()"> Click Me </button>
    </div>
</body>
</html>

本文标签: How to get Value of Password input JavascriptStack Overflow