我创建了一个表,允许用户编辑/更正数据。该表填充得很好,并且是为了使该表尽可能易于使用。我添加了下拉框,当他们需要更改任何信息时可以从中进行选择。
JS 文件是:
for(y=0;y<data.defect2.length; y++) {
myselectoptions += '<option value="'+data.defect_id[y]+'"' +
(data.defect_id[y]==data.defect[y] ? ' selected="selected"' : '') +
'>'+data.defect2[y]+'</option>';
}
if (data.isbn2 === null) {
$("#inventoryUpdate").append('<tr><td>No Records Found</td></tr>');
} else {
for(var x=0;x<data.isbn2.length;x++) {
$("#inventoryUpdate").append('<tr><td id="tableSKU">'+data.sku[x]+'</td><td id="tableISBN">'+data.isbn2[x]+
'</td><td id="tableQuantity">'+data.quantity[x]+
'</td><td><select id="tableDefect">'+myselectoptions+//'" selected="'+data.defect[x]+'">'+myselectoptions2+
'"</select></td><td><select id="tableSource">'+sourceoptions+
'"</select></td><td><select id="tableFeature">'+featureoptions+
'"</select></td><td><select id="tableWater">'+wateroptions+
'"</select></td><td><select id="tableLocation">'+locationoptions+
'"</select></td><td><input type="text" id="tableProcessDate" value="'+data.processDate[x]+
'"/></td><td><select id="tableBookType">'+bookoptions+
'"</select></td><td><select id="tableCreatedBy">'+useroptions+
'"</select></td><td><select id="tableModifiedBy">'+useroptions+
'"</select></td></tr>');
}
$("#inventoryUpdate").trigger("update");
}
这一切都有效,只是我不能将其默认为数据库查询中选定的项目。所选项目是表中最后一项的值。 关于如何实现这一点有什么想法或者是不可能做到吗?
最佳答案
我建议不要在初始渲染期间尝试添加 selected
属性,而是建议在之后运行它。像这样的事情:
var selectedValue = 'option[value="'+data.defect_id[y]+'"]';
$('#tableDefect').find(selectedValue).attr("selected",true).end();
关于jquery - 使用jquery tablesorter插件,有一个下拉框,其中包含选定的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13826207/