简体   繁体   中英

How do I iterate through a table row to get all elements values?

I am creating a table based on code that will create x rows and then each row will have a rowspan="3" Each row has different objetcs (input, select, etc)

         - text | input | select | select | input | text
Event 1  - text | input | select | select | input | text 
         - text | input | select | select | input | text  

Event 2

I will ultimately save the "Event 1" and rows to a MySQL DB

How can I iterate through the 3 rows and get the , text, input and selected option in one array?

I have tried code , for example,

$('select.so').filter(':visible').children(':selected')

which returns the selected results.

How can I get a list of all elements?

 function loadTable() {
 var event = new Array();
 event[0] = "200 Yard Medley Relay"
event[1] = "200 Yard Free Style"
$('#sections').append('<table></table>');
var table = $('#meetTable').children();
 for (i = 0; i < event.length; i++) {
table.append('' +
 '<tr id= class="header expand" >' +
 '<th rowspan="3" id="eventNames">' + event[i] + '</th>' +
'<td contenteditable="true">' +
'<select id ="s1" name="swimmerOption" class="so"><?php echo $options; ?>' +
 '</select>' +
 '</td> ' +
   '<td>' +
 '<select class="lane"> ' +
 '<option value="1">1</option>' +
 '<option value="2">2</option>' +
'<option value="3">3</option>' +
 '</select> </td>' +
'<td contenteditable="true" value="">' +
'<input type="text" style="width:70px;text-align: center" class="time" autocomplete="off">' +
'</td> ' +
'<td> ' +
'<select class="place"> ' +
'<option value="1">1</option> ' +
'<option value="2">2</option>' +
'<option value="3">3</option>' +
'</select>' +
'</td>' 
'</tr> ' +
'<tr>')
 }
 }

How about this one:

    var $elements;
    $('tr').each(function(){
       $elements=$(this).find('input, select option:selected');
    });
    var jsArr=$.makeArray($elements); //DOM elements array

You may try something like this. The implementation is very straight and simple.

<table>
    <tr>
        <td><label>Row1</label></td>
        <td><input type="text"></input></td>
        <td><select><option>1</option><option>2</option></select></td>
    </tr>
     <tr>
        <td><label>Row2</label></td>
        <td><input type="text"></input></td>
        <td><select><option>1</option><option>2</option></select></td>
    </tr>
</table>
<br>
    <input type="button" value="Get Data"/>
    <div id="values"></div>

var values;

$("input:button").click(function()
{
    values = "";
    $("table tr").each(function()
    {
        $(this).find("td").each(function(index)
        {
            if($(this).find("label").text() != undefined)
              values += $(this).find("label").text() + ";";
           if($(this).find("input").val() != undefined)
            values += $(this).find("input").val() + ";";
            if($(this).find("select option:selected").text() != "")
            values += $(this).find("select option:selected").text() + ";";
        });
    });

    values = values.replace(/\;;/g,";");
    $("#values").text(values);
});

http://jsfiddle.net/yqr6zgo6/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM