简体   繁体   English

如何基于保存的选择框值更改选择框值

[英]how to change select box value based on saved select box value

if (itemPrice != null) {
        var option = null;
        for (var i = 0; i < itemPrice.d.length; i++) {
            option += '<option value=' + itemPrice.d[i].ListNum + '>' + itemPrice.d[i].ListName + '</option>';
        }

    } else {
        SessioExp(response.statusText);
    }

    tblRow += '<td style="width:20%"><div id="' + d.ItemCode + '"><select class="customSelect" name="dropdown" >' + option + '</select></div></td>';
    $.each(itemLinked, function (k, v) {
        if (d.ItemCode == v.ITEMCODE) {
           //Here set v.PRICELIST TO OPTION VALUE
            if (v.ISLINKED) {
                tblRow += '<td style="width:10%" align="center"><a href="#"><span id="existingData" class="fa fa-times" aria-hidden="false" style="display: none;"></span></a></td>';
                tblRow += '<td style="width:10%" align="center"><input type="checkbox" class="chkLinked" checked /></td>';
                flage = false;
            }
        }
    });

I want set select box value when condition true if (d.ItemCode == v.ITEMCODE) 我想在条件为true时设置选择框值,如果(d.ItemCode == v.ITEMCODE)

tblRow += '<td style="width:20%"><div id="' + d.ItemCode + '"><select class="customSelect" name="dropdown" >' + option + '</select></div></td>';

Here we want to show first saved value. 在这里我们要显示第一个保存的值。

Assuming that your generated options have been appended to the html, you can simply call the JS function optionID.selected = true 假设您生成的选项已附加到html,则只需调用JS函数optionID.selected = true

So for that you will need to define ids to all your options 因此,您需要为所有选项定义ID

// assuming that all your values are unique
for (var i = 0; i < itemPrice.d.length; i++) {
   option += '<option id=' + itemPrice.d[i].ListNum + ' value=' + itemPrice.d[i].ListNum + '>' + itemPrice.d[i].ListName + '</option>';
}
//pass the option id here
//P.S you can only call this funtion only if the options have been appended to the html
function defineOption(optionID) {
     document.getElementById(optionID).selected = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM