开发者

How to use jQuery to display labels and input values?

开发者 https://www.devze.com 2023-01-21 22:36 出处:网络
I need a jQuery function that\'ll go through paragraphs with开发者_如何学Go the following structure:

I need a jQuery function that'll go through paragraphs with开发者_如何学Go the following structure:

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

The function should use the label text as input value.

Thanks!


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

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


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

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


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

http://jsfiddle.net/Fveph/


$("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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号