我正在为我的网站创建一个 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>
我知道这不是服务器端问题。 $("input[type=username]").val()
的返回值为undefined
,而$("input[type =password]").val()
是输入的值。
谢谢大家! :)
最佳答案
username
不是有效的 type
属性值。我猜您想要的是 text
,这是浏览器在遇到无法识别的类型时默认使用的内容。因此,当您的代码运行时,input
的类型实际上是text
,而不是username
。
参见 specification获取有效 type
属性值的完整列表。
旁注 - 将您的 jQuery 版本升级到 1.3.2 或(最好)以上版本将解决此问题。我会尽快离开你目前使用的古老的 1.2.6!
查看 working version (jQuery 1.7.2) here .将包含的 jQuery 版本更改为 1.2.6,它将记录 undefined
,如您在问题中所说。
关于javascript - $ ("input[type=username]").val() 返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11511099/