如何使用jQuery显示标签和输入值?

发布于 2024-09-28 20:25:02 字数 199 浏览 2 评论 0原文

我需要一个 jQuery 函数,它将遍历具有以下结构的段落:

<p>
  <label>some label</label>
  <input type="text" value=""/>
</p>

该函数应使用标签文本作为输入值。

谢谢!

I need a jQuery function that'll go through paragraphs with the following structure:

<p>
  <label>some label</label>
  <input type="text" value=""/>
</p>

The function should use the label text as input value.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

美羊羊 2024-10-05 20:25:02
$('p > label + input').val(function() { return $(this).prev().text(); });

示例: http://jsfiddle.net/D392c/

$('p > label + input').val(function() { return $(this).prev().text(); });

Example: http://jsfiddle.net/D392c/

锦上情书 2024-10-05 20:25:02

您可以使用 .val() 和函数来完成此操作,如下所示:

$("p input").val(function() { return $(this).prev().text(); });

You can do it using .val() with a function, like this:

$("p input").val(function() { return $(this).prev().text(); });
瞄了个咪的 2024-10-05 20:25:02
$('p').each(function() {
    $(this).children('input').val($(this).children('label').text());
});

http://jsfiddle.net/Fveph/

$('p').each(function() {
    $(this).children('input').val($(this).children('label').text());
});

http://jsfiddle.net/Fveph/

云柯 2024-10-05 20:25:02

$("p > label") 要解析结构,然后您可以使用 html() 来获取值,但这会返回第一个节点的值...您想对标签文本做什么?

$("p > label") To parse the structures, then you can use html() to get the value, but that returns the first node's value... What do you want to do with the label text?

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