使用 jQuery 获取失去焦点时在文本框中输入的文本

发布于 2024-12-27 21:11:32 字数 773 浏览 3 评论 0原文

如何获取下面文本框中输入的值?

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉絮湮 2025-01-03 21:11:32
$('input[name=' + id+ ']').blur(function () {
    alert($(this).val());
});

请注意,您的 var 名称是 id,但它的值是 name...为什么?

我将更改为:

var id = $("td:contains('1.')").next().next().children().eq(0).attr('id');

$('#' + id).blur(function () {
    alert($(this).val());
});

JSFiddle 示例

您必须“转义” < code>$ 与 \\$ 将其与 jquery 选择器一起使用。

$('input[name=' + id+ ']').blur(function () {
    alert($(this).val());
});

Notice that your's var name is id but it's value is name... why?

I would change to:

var id = $("td:contains('1.')").next().next().children().eq(0).attr('id');

$('#' + id).blur(function () {
    alert($(this).val());
});

JSFiddle example

You have to "escape" the $ with \\$to use it with jquery selectors.

喜你已久 2025-01-03 21:11:32

$('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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文