使下拉项目动态选择为true

时间:2017-11-05 10:40:04

标签: javascript jquery html

我使用以下代码行

动态选择下拉项目
for(var i in data) {

    var book_id = data[i]['book_id'];
    var indivudvalbookdetails = data[i]['indivudvalbookdetails'];
    var id=1;

    for(var j in indivudvalbookdetails) {
       if(indivudvalbookdetails[j]['status'] == undefined || indivudvalbookdetails[j]['status'] == "")
        {
            $('.library_info_tbl tbody').append('<tr>' +
            '<td class="text-center centeralign"> ' + data[i]['subject'] + '</td>' +
            '<td class="text-center centeralign"> ' + data[i]['title'] + ' </td>' +
            '<td class="text-center centeralign"> ' + data[i]['isbn'] + '</td>' +
            '<td class="text-center centeralign"> ' + data[i]['author'] + ' </td>' +
            '<td class="text-center centeralign"> ' + indivudvalbookdetails[j]['acquisitionno'] + '</td>' +
            '<td class="text-center centeralign"><div class="btn-group">' +
            '<input type="text" class="hide" id="acquisitionno' + id + '" value="' + indivudvalbookdetails[j]['acquisitionno'] + '" class="form-control">' +
            '<select id="status' + id + '" class="form-control">' +
            '<option value="Select">Select</option>'+
            '<option value="Damaged"' + 
              indivudvalbookdetails[j]['status'] == "Damaged" ? 'selected="true"': 'selected="false">Damaged</option>'+
            '<option value="Lost"' + 
              indivudvalbookdetails[j]['status'] == "Lost" ? 'selected="true"' : ' selected="false">Lost</option>' +
            '</select>'+
            //'<input type="text" id="status' + id + '" class="form-control">' +
            '</div></td>' +
            '</tr>');
            id++;
        }

    }

它以html格式显示代码:

"selected="false">Lost selected="false">Lost 
 selected="false">Lost selected="false">Lost selected="false">Lost"

1 个答案:

答案 0 :(得分:0)

字符串终止不正确,if条件也是如此。

检查以下代码:

&#13;
&#13;
var id = 'select';
var indivudvalbookdetails = [{
status: 'Damaged'
}]
var j = 0;

var select = '<select id="status' + id + '" class="form-control">' +
    '<option value="Select">Select</option>' +
    '<option value="Damaged"' +
    (indivudvalbookdetails[j]['status'] == "Damaged" ? 'selected="selected"' : '') + '>Damaged</option>' +
    '<option value="Lost"' +
        (indivudvalbookdetails[j]['status'] == "Lost" ? 'selected="selected"' : '')  + '>Lost</option>' +
'</select>' 


document.getElementById('main').innerHTML = select;
&#13;
<div id="main"></div>
&#13;
&#13;
&#13;