<select id="<span style="font-family: Arial, Helvetica, sans-serif;">mySelect</span><span style="font-family: Arial, Helvetica, sans-serif;">"></span>
<option value="shanghai">上海</option>
<option value="beijing">北京</option>
</select>
根据value 设置select 的 option 选中
<script>
function jsSelectItemByValue(objSelect,objItemText) {
for(var i=0;i<objSelect.options.length;i++) {
if(objSelect.options[i].value == objItemText) {
objSelect.options[i].selected = true;
break;
}
}
}
var selectedValue = "shanghai";
jsSelectItemByValue(document.getElementById("mySelect"),selectedValue);
<script>
根据text 文本 设置selec t的 option 选中
<script>
function jsSelectItemByValue(objSelect,objItemText) {
for(var i=0;i<objSelect.options.length;i++) {
if(objSelect.options[i].text == objItemText) {
objSelect.options[i].selected = true;
break;
}
}
}
var selectedValue = "上海";
jsSelectItemByValue(document.getElementById("mySelect"),selectedValue);
<script>