开发者

How to loop through all the form elements inside a table row using jquery?

How do I get all the form elements inside a table row? Each row can have many tds with each td hav开发者_如何学Going any number of input or select elements.

Please help.


You should be able to do this:

$('#myTableRow').find('input, select, textarea').each(function()
{
});


Try this -

$("table tr :input").each(function () {
    //your logic here
    //alert(this.tagName)
}) 

Working demo - http://jsfiddle.net/ipr101/qMS7P/


I would suggest:

$('table input, table textarea, table select').each(function() { ... });


For table rows you could select all inputs like this:

var myInputFields = $("#myTable tr input[type='text']");

This will select only inputs, no matter how many levels deep into a table row - so you can have rows which have div>s, p>s and other stuff wrapped around the input>s.

You could use jQuery.each or a simple for i=0->myInputFields.length to loop through all the input fields.

myInputFields.each(function(i,v){
   var v = $(v);
   console.debug(v.html(),v.val());
});

You could extend the selector for more input/textareas etc easily like the example in simoncereska's answer.


If you want the inputs in a specific row:

HTML

<table>
  <tr id="the_row">
    <td><input type="text" value="1"/></td>
    <td><input type="text" value="2"/></td>
  </tr>
  <tr>
    <td><input type="text" value="3"/></td>
    <td><input type="text" value="4"/></td>
  </tr>
</table>

Javascript

$("#the_row :input").each(function () {
  console.log(this.value);
});

(Note: The :input selector matches all input, select, textarea and button elements)

DEMO: http://jsfiddle.net/WuamV/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜