$("input [type = username]").val()返回undefined

Pat*_*ray 2 html javascript ajax jquery

我正在为我的网站创建一个AJAX日志表格; 但是,我遇到了一些我认为是jQuery中的错误的东西.

代码示例:

<!DOCTYPE html>
<html>
    <head>
        <title>Log In</title>
    </head>
    <body>
        <div class="errors"></div>
        <input type="username" placeholder="username">
        <input type="password" placeholder="password">
        <input type="submit" value="Log In">
    </body>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
    <script>
        $('input[type=submit]').click(function(){
            $(".errors").load(window.location.protocol + "//" + window.location.host + "?username=" + $("input[type=username]").val() + "&password=" + $("input[type=password]").val());
            return false;
    });
    </script>
</html>
Run Code Online (Sandbox Code Playgroud)

我知道这不是服务器端问题.返回值为$("input[type=username]").val()is undefined,而返回值为$("input[type=password]").val()输入值.谢谢大家!:)

Jam*_*ice 11

username不是有效的type属性值.我猜你想要的text是,这就是浏览器在遇到无法识别的类型时会默认的内容.因此,当您的代码运行时,input实际上有一种类型text,而不是username.

有关有效属性值的完整列表,请参阅规范type.


附注 - 将您的jQuery版本升级到1.3.2或(最好)以上将解决此问题.我会尽快离开你目前正在使用的古代1.2.6!

在这里查看工作版本(jQuery 1.7.2).将包含的jQuery版本更改为1.2.6,它会记录undefined,正如您在问题中所说的那样.