[英]how to append value in select tag inside dynamic table
我使用json響應添加表行數據。 這是我的代碼
var i;
for (i = 0; i < result.length; i++) {
$.get('LoadserviceSplit', {
"sectcode" : result[i]
},
function (jsonResponse) {
if (jsonResponse != null) {
var table2 = $("#table_assign");
$.each(jsonResponse, function (key, value) {
var rowNew = $("<tr><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
rowNew.children().eq(0).text(value['serviceId']);
rowNew.children().eq(1).text(value['title']);
rowNew.children().eq(2).html('<input type="text" id="date_set" name="date_set"/>');
rowNew.children().eq(3).html('<input type="text" id="date_set1" name="date_set1"/>');
rowNew.children().eq(4).html('<input type="text" id="date_set2" name="date_set2"/>');
rowNew.children().eq(5).html('<select class="status1" id="status1">');
rowNew.appendTo(table2);
});
}
});
var pass_unit_code = "001";
$.get('LoadDivisionCodeServlet', { //call LoadDivisionCodeServlet controller
unitCode : pass_unit_code //pass the value of "sample" to unitCode:
}, function (jsonResponse) { //json response
var select = $('#status1'); //select #status1 option
select.find('option').remove(); //remoev all item in #divcode option
$.each(jsonResponse, function (index, value) {
$('<option>').val(value).text(value).appendTo(select); //response from JSON in array value{column:value,column:value,column:value}
});
});
}
除了選擇標簽部分,它工作正常。 只有表格的第一行才有價值。 其余的沒有價值。 我希望表格內的所有下拉列表都具有相同的值..有人可以幫我嗎?
看一眼
rowNew.children().eq(5).html('<select class="status1" id="status1">');
您正在$.each
創建新的select
元素,並$.each
分配相同的ID,即status1
。
然后,您要選擇id
為status1
的select
元素,例如
var select = $('#status1'); //select #status1 option
因此,將僅select
第一個select
元素。
編輯:
您的問題尚不完全清楚。
但是,這是您可以在每個<td>
內部添加不同的ID進行選擇的方法
取代這個
rowNew.children().eq(5).html('<select class="status1" id="status1">');
用類似的東西
rowNew.children().eq(5).html('<select class="status1" id="status'+key+'">');
因此,這將具有不同的ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.