为什么我的javascript只检索一个输入元素值?

时间:2013-11-08 11:21:46

标签: javascript html forms input field

我正在尝试从表单中的元素中检索值。但出于某种原因,我只能从一个元素中获得价值。

这是我的HTML代码:

<form name="registerform" class="registerform">
        <label for="usernamefield">Username: </label>
        <br>
        <input type="field" id="usernamefield"></input>
        <br>
        <label for="emailfield">Email: </label>
        <br>
        <input type="email" name="emailfield" id="emailfield"></input>
        <br>
        <label for="passwordfield">Password: </label>
        <br>
        <input type="password" name="passwordfield" id="passwordfield"></input>
        <br>
        <label for="passwordrepeatfield">Repeat Password: </label>
        <br>
        <input type="password" name="passwordrepeatfield"></input>
        <br>
        <!--Check field-->
        <br>
        <input type="checkbox" name="agreementbox" id="agreementbox"></input>
        <label for="agreementbox" id="termslink">I agree to the </label>
        <a href="" id="termslink">terms</a>
        <br>
        <input type="button" id="registerbutton" value="Register" onclick="registerUser()">

    </form>

这是javascript代码:

function registerUser()
{

var username = document.getElementById("usernamefield").value;
var email = document.getElementById("emailfield").value;
var password = document.getElementById("passwordfield").value;

console.log("Username: " + username);
console.log("Email: " + email);
console.log("Password: " + password);

if (window.XMLHttpRequest)
{
    // Code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else
{
    // Code for IE6, IE5
    xmlhttp = ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("POST",
             "RegisterUser.php?username=" + username
             + "&email=" + email
             + "&password" + password,
             true);
xmlhttp.send();


}

在控制台中,我只看到输入电子邮件字段的值。

希望有人可以帮助我。在此先感谢:)

1 个答案:

答案 0 :(得分:0)

感谢您对输入类型进行改进

我弄明白了这个问题。显然我设法有重复的输入元素具有相同的ID。

感谢大家的帮助:)