![](/img/trans.png)
[英]How do I iterate through a table and extract the values from a row and compare with datatable
[英]How do I iterate through a table row to get all elements values?
我正在基於將創建x行的代碼創建表,然后每行將具有rowpan =“ 3”每行具有不同的objetc(輸入,選擇等)
- text | input | select | select | input | text
Event 1 - text | input | select | select | input | text
- text | input | select | select | input | text
Event 2
我最終將“事件1”和行保存到MySQL數據庫
如何遍歷3行並在一個數組中獲取,文本,輸入和選擇的選項?
我嘗試過代碼,例如,
$('select.so').filter(':visible').children(':selected')
它返回選定的結果。
如何獲取所有元素的列表?
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>')
}
}
這個怎么樣:
var $elements;
$('tr').each(function(){
$elements=$(this).find('input, select option:selected');
});
var jsArr=$.makeArray($elements); //DOM elements array
您可以嘗試類似的方法。 該實現非常簡單直接。
<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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.