$.each(result, function() {
$.each(this, function(k, item) {
var s = $("<select id=\"selectId\" name=\"selectName\" />");
html += '<tr>';
html += '<td>' + item.id + '</td>';
html += '<td>' + item.admno + '</td>';
html += '<td>' + item.stud_name + '</td>';
html += '<td>' + item.remark + '</td>';
html+= '<td>'+s+'</td>';
for(var val in item)
{
$("<option />", {value: val.id, text: item[val.stud_name]}).appendTo(s);
}
html += '</tr>';
});
});
$('.tbody').html(html);
在我试图使用$(s).wrap附加注释之后,我必须附加select标签。帮助我使用jQuery动态追加选择
答案 0 :(得分:0)
尝试一下:
var result = [];
var obj = {};
obj.id = 1;
obj.admno = '123';
obj.stud_name = 'Ali';
obj.remark = 78;
result.push(obj);
var obj2 = {};
obj2.id = 1;
obj2.admno = '235';
obj2.stud_name = 'Ahmad';
obj2.remark = 78;
result.push(obj2);
$.each(result, function( index, item ) {
var html = '';
html += '<tr>';
html += '<td>' + item.id + '</td>';
html += '<td>' + item.admno + '</td>';
html += '<td>' + item.stud_name + '</td>';
html += '<td>' + item.remark + '</td>';
var select = "<select id='selectId' name='selectName' >";
$.each(result, function(i,val)
{
select +=
"<option value = '" + val.id + "'>" + val.stud_name + " </option>";
}
);
select += "</select>";
html += '<td>'+select+'</td>';
html += '</tr>';
$('.tbody').append(html);
});
答案 1 :(得分:0)
尝试
$.each(result, function() {
$.each(this, function(k, item) {
html += '<tr>';
html += '<td>' + item.id + '</td>';
html += '<td>' + item.admno + '</td>';
html += '<td>' + item.stud_name + '</td>';
html += '<td>' + item.remark + '</td>';
html+= '<td>';
html += "<select id=\"selectId\" name=\"selectName\" >";
for(var val in item)
{
html+="<option value="+val.id+">item[val.stud_name]</option>",
}
html+= '</select>';
html+= '</td>';
html += '</tr>';
});
});
$('.tbody').html(html);