Javascript 如何根据保存的选择框值更改选择框值 if(itemPrice!=null){ var选项=null; 对于(变量i=0;i

Javascript 如何根据保存的选择框值更改选择框值 if(itemPrice!=null){ var选项=null; 对于(变量i=0;i,javascript,jquery,Javascript,Jquery,如果(d.ItemCode==v.ItemCode),我希望在条件为true时设置选择框值 tblRow+=''选项+''; 这里我们要显示第一个保存的值。假设生成的选项已附加到html中,您只需调用JS函数optionID.selected=true 因此,您需要为所有选项定义ID tblRow += '<td style="width:20%"><div id="' + d.ItemCode + '"><select class="customSelect"

如果(d.ItemCode==v.ItemCode),我希望在条件为true时设置选择框值

tblRow+=''选项+'';

这里我们要显示第一个保存的值。

假设生成的选项已附加到html中,您只需调用JS函数
optionID.selected=true

因此,您需要为所有选项定义ID

tblRow += '<td style="width:20%"><div id="' + d.ItemCode + '"><select class="customSelect" name="dropdown" >' + option + '</select></div></td>';
//假设所有值都是唯一的
对于(变量i=0;i
So…..您是说需要显示动态生成的选项中预定义的选定选项吗?是的,我想使用预定义的选定选项您是否先附加html,然后调用函数?是的,在附加'blRow+=''+选项+''后,我想将选项值更改为保存的值,即if中的v.PRICELIST(d.ItemCode==v.ItemCode)条件。通过附加html,我的意思是创建DOM,而不是将它们保存在js中。因为除非它们存在于DOM中,否则js将无法运行
tblRow += '<td style="width:20%"><div id="' + d.ItemCode + '"><select class="customSelect" name="dropdown" >' + option + '</select></div></td>';
// 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;
}