问题
I have a table that has drop down boxes to allow the user to select different options. I need to show what the default selection is when the table is loaded. Here is my code so far:
//get info for defect dropdown
var myselectoptions = '';
for(y=0;y<data.defect2.length; y++)
{
defect2 = data.defect2[y];
defect3 = defect2.substring(0,30);
myselectoptions += '<option value="'+data.defect_id[y]+'">'+defect3+'</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><input type="text" id="tableQuantity" value="'+data.quantity[x]+
'"/></td><td><select id="tableDefect">'+myselectoptions+
'"</select></td><td><input type="text" id="tableSource" value="'+data.source[x]+
'"/></td><td><input type="text" id="tableFeature" value="'+data.feature[x]+
'"/></td><td><input type="text" id="tableWater" value="'+data.water[x]+
'"/></td><td><input type="text" id="tableLocation" value="'+data.location[x]+
'"/></td><td><input type="text" id="tableProcessDate" value="'+data.processDate[x]+
'"/></td><td><input type="text" id="tableBookType" value="'+data.booktype[x]+
'"/></td><td><input type="text" id="tableCreatedBy" value="'+data.created[x]+
'"/></td><td><input type="text" id="tableModifiedBy" value="'+data.modified[x]+
'"/></td></tr>');
}
$("#inventoryUpdate").trigger("update");
I know that if it was a regular drop down I could use selected="selected"(which I tried to do,
'<option value="'+data.defect_id[y]+'"selected="'+data.defect[y]+'">...
), but how to i do that in this case?
EDIT: The data for the selected is in data.defect[x]
回答1:
Try using a ternary operator (example):
myselectoptions += '<option value="'+data.defect_id[y]+'"' +
(data.defect[y] ? ' selected="selected"' : '') + '>'+defect3+'</option>';
来源:https://stackoverflow.com/questions/11990972/use-select-tags-with-table-sorter-plug-in-and-have-it-default-to-a-given-value