最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery get input text when using .each() function - Stack Overflow

programmeradmin0浏览0评论

I am using JQuery .each() on a list and am wondering how to retrieve the text from an input field within the list item, the code looks like:

<ul id="foo">
  <li id="1">
    <input type="text" />
  </li>
  <li id="2">
    <input type="text" />
  </li>
</ul>

I then need to get the id and text to send to an AJAX call

$(#foo li).each( function() {
  id = this.id; // Works fine
  mytext = ???; // The bit i'm stuck with
  ...
});

There is only one input control per list item.

I am using JQuery .each() on a list and am wondering how to retrieve the text from an input field within the list item, the code looks like:

<ul id="foo">
  <li id="1">
    <input type="text" />
  </li>
  <li id="2">
    <input type="text" />
  </li>
</ul>

I then need to get the id and text to send to an AJAX call

$(#foo li).each( function() {
  id = this.id; // Works fine
  mytext = ???; // The bit i'm stuck with
  ...
});

There is only one input control per list item.

Share Improve this question asked Jan 20, 2012 at 18:00 MattPMattP 2,8732 gold badges33 silver badges43 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2
$('#foo li').each( function() { 
  id = this.id; // Works fine
  mytext =  $(this).find('input[type=text]').val();  
  $('#foo').after('<div> text in input = ' +mytext + '</div>');
}); 

here it is working http://jsfiddle/J8ZUT/1/

$(#foo li).each( function() {
  id = this.id; // Works fine
  mytext = $(this).find(':input').val(); // <------------
  ...
});

Note that this will also work with selects and other "input" types, also this will only work when there is only a single :input within the list element.

mytext = $(this).find('input').val();
发布评论

评论列表(0)

  1. 暂无评论