How do I programmatically set the value of a select box element using JavaScript?

后端 未结 17 1901
轮回少年
轮回少年 2020-11-22 06:33

I have the following HTML

17条回答
  •  别那么骄傲
    2020-11-22 06:55

    Not answering the question, but you can also select by index, where i is the index of the item you wish to select:

    var formObj = document.getElementById('myForm');
    formObj.leaveCode[i].selected = true;
    

    You can also loop through the items to select by display value with a loop:

    for (var i = 0, len < formObj.leaveCode.length; i < len; i++) 
        if (formObj.leaveCode[i].value == 'xxx') formObj.leaveCode[i].selected = true;
    

提交回复
热议问题