使用 jQuery 获取失去焦点时在文本框中输入的文本
如何获取下面文本框中输入的值?
<tr>
<td class="TextBold" >1.</td>
<td class="TextBold" >Content for Question 1:</td>
<td class="TextBold" >
<input name="ctl00$DefaultContent$ctl2" type="text" class="TextNormal" />
</td>
</tr>
这给了我文本框的名称:
var idT = $("td:contains('1.')").next().next().children().eq(0).attr('name');
现在我想编写函数来给出在文本框中输入的值。
类似的东西:
$('input[name^=ctl00$DefaultContent$ctl2]').blur(function () {
alert($(this).val());
});
这对我有用:
$('input[name="' + idT + '"]').blur(function () {
alert($(this).val());
});
How can I get the value entered in the textbox below?
<tr>
<td class="TextBold" >1.</td>
<td class="TextBold" >Content for Question 1:</td>
<td class="TextBold" >
<input name="ctl00$DefaultContent$ctl2" type="text" class="TextNormal" />
</td>
</tr>
This gives me the name of the textbox:
var idT = $("td:contains('1.')").next().next().children().eq(0).attr('name');
Now I would like to write function which will give the value entered in the textbox.
Something like:
$('input[name^=ctl00$DefaultContent$ctl2]').blur(function () {
alert($(this).val());
});
This worked for me:
$('input[name="' + idT + '"]').blur(function () {
alert($(this).val());
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,您的 var 名称是 id,但它的值是 name...为什么?
我将更改为:
JSFiddle 示例
您必须
“转义”
< code>$ 与\\$
将其与 jquery 选择器一起使用。Notice that your's var name is
id
but it's value is name... why?I would change to:
JSFiddle example
You have to
"escape"
the$
with\\$
to use it with jquery selectors.$('input[name="ctl00$DefaultContent$ctl2"]').val()
应该为您提供文本字段的值,但“ctl00$DefaultContent$ctl2”真的是名称吗?请参阅此 jsFiddle
$('input[name="ctl00$DefaultContent$ctl2"]').val()
should give you the value of the text field but is "ctl00$DefaultContent$ctl2" really the name?See this jsFiddle