我有这个代码,我想将所选项目(百万(s))更改为除此之外的其他内容。我该怎么做?
<td>
<select name='list_" + i + "' id='list'>
<option value='0'>Trillion(s)</option>
<option value='1' selected='selected'>Billion(s)</option>
<option value='2'>Million(s)</option>
<option value = '3'>Below One Million</option>
</select>
</td>
以下是我尝试更改所选选项的代码行
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
if(document.getElementById("list_"+counter).innerHTML=="true"){
document.getElementById("c" + counter).value = myObject[key];
counter++;
}else{
if(myObject[key]>=1.0E12){
myObject[key]=myObject[key]/1.0E12;
//document.getElementById("list").value=counter;
//document.getElementById("list").getElementsbyTagName('option')[counter].value=counter;
counter++;
}
document.getElementById("c" + counter).value = myObject[key];
}
}
}
答案 0 :(得分:0)
我可能理解你的要求是错误的。
如果您想更改所选项目,则只需更改“已选择”属性即可。
假设我想将其更改为Trillion,那么这就是代码的样子:
<select name='list_" + i + "' id='list'>
<option value='0' selected='selected'>Trillion(s)</option>
<option value='1' >Billion(s)</option>
<option value='2'>Million(s)</option>
<option value = '3'>Below One Million</option>
</select>
现在假设您要通过纯javascript(NO JQUERY)
更改所选项目以下是完整示例,单击按钮时所选项目将会更改。 注意:这只是改变索引的问题。
<head>
<title> Changing selected option in drop-down menu in JS</title>
<script type="text/javascript">
function changeSelected(){
document.getElementById("list").selectedIndex = 1;
};
</script>
</head>
<body>
<p> Test string....</p>
<td>
<select name='list_" + i + "' id='list'>
<option value='0' selected='selected'>Trillion(s)</option>
<option value='1' >Billion(s)</option>
<option value='2'>Million(s)</option>
<option value = '3'>Below One Million</option>
</select>
<button onclick="changeSelected()">Try It</button>
</td>
</body>