【问题标题】:jquery can't get the value from an input elementjquery 无法从输入元素中获取值
【发布时间】:2012-12-29 06:10:12
【问题描述】:
我正在使用 jquery 尝试检索文本输入的值,但它不起作用。谁能看到我做错了什么? http://jsfiddle.net/silvajeff/rJNLr/
<input id="mytest2" value="123"/>
<button id="findRow">Find Row</button>
$("#findRow").click(function() {
var theVal = $("#myTest2").val();
alert(theVal);
});
我向大家道歉,但我最初发布的内容只是我在分解问题以简化问题时的一个错字。我将不得不再次重新发布,只是这次我会输入未简化的代码here。不过,我将把这个问题留在这里,并添加一个区分大小写的标签,因为我仍然认为这些对于可能有问题的其他人来说是有价值的解决方案。
【问题讨论】:
标签:
jquery
select
text
input
case-sensitive
【解决方案1】:
您使用了错误的 ID myTest2,它不存在使用 mytest2。 Javascript 区分大小写,因此您需要小心。
Live Demo
$("#findRow").click(function() {
var theVal = $("#mytest2").val();
alert(theVal);
});
【解决方案2】:
将$("#myTest2") 更改为$("#mytest2")
【解决方案3】:
您错误地提及了您的字段的“id”值。
id 值在 jQuery 中区分大小写
因此,改变
myTest2 到
我的测试2
【解决方案4】:
将id="mytest2" 更改为id="myTest2"
试试这个:
<input id="myTest2" value="123"/>
<button id="findRow">Find Row</button>
$("#findRow").click(function() {
var theVal = $("#myTest2").val();
alert(theVal);
});
这意味着 id 区分大小写。