[英]how to append value in select tag inside dynamic table
im adding table row data using json response. 我使用json响应添加表行数据。 here is my code
这是我的代码
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}
});
});
}
it works fine except the select tag part. 除了选择标签部分,它工作正常。 only the first row of table have value.
只有表格的第一行才有价值。 the rest has no value.
其余的没有价值。 i want all drop-down list inside the table has same value.. can anyone help me about this.
我希望表格内的所有下拉列表都具有相同的值..有人可以帮我吗?
Take a look at 看一眼
rowNew.children().eq(5).html('<select class="status1" id="status1">');
You're creating new select
elements in a $.each
and assigning the same id, that is status1
to all of them. 您正在
$.each
创建新的select
元素,并$.each
分配相同的ID,即status1
。
Then you're selecting the select
element that has an id
of status1
like 然后,您要选择
id
为status1
的select
元素,例如
var select = $('#status1'); //select #status1 option
Therefore, only the first select
element will be selected. 因此,将仅
select
第一个select
元素。
EDIT: 编辑:
Your question is not completely clear. 您的问题尚不完全清楚。
However, this is how you can add different Id for select inside each of your <td>
但是,这是您可以在每个
<td>
内部添加不同的ID进行选择的方法
Replace this 取代这个
rowNew.children().eq(5).html('<select class="status1" id="status1">');
With something like 用类似的东西
rowNew.children().eq(5).html('<select class="status1" id="status'+key+'">');
So this will have different Ids. 因此,这将具有不同的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.