use select tags with table sorter plug in and have it default to a given value

你说的曾经没有我的故事 提交于 2019-12-11 18:56:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!