HTML SELECT - 使用 JavaScript 按 VALUE 更改所选选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12265596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML SELECT - Change selected option by VALUE using JavaScript
提问by Dilip Raj Baral
There can be many options in a SELECT dropdown.
SELECT 下拉列表中可以有很多选项。
<select id="sel">
<option value="car">1</option>
<option value="bike">2</option>
<option value="cycle">3</option>
...
</select>
I'm creating a Update Profilepage where a user's profile is retrieved from the database and a form is shown with those values. When it comes to SELECT
dropdown, there are many options. So, its tedious test all values
我正在创建一个更新配置文件页面,其中从数据库中检索用户的配置文件,并显示带有这些值的表单。说到SELECT
下拉菜单,有很多选择。所以,它繁琐的测试所有值
if (value == '1')
echo "<option selected value='car'>1</option>";`
So, I want to have the corresponding option selected from its value. Like when I do something like sel.value = 'bike'
using JavaScript the option 2should be selected.
所以,我想从它的值中选择相应的选项。就像当我sel.value = 'bike'
使用 JavaScript做类似的事情时,应该选择选项2。
回答by Michal Klouda
回答by Trevor
If you are using jQuery:
如果您使用 jQuery:
$('#sel').val('bike');
回答by Vijay
try out this....
试试这个....
using javascript
使用 JavaScript
?document.getElementById('sel').value = 'car';??????????
using jQuery
使用 jQuery
$('#sel').val('car');
回答by Vijay
document.getElementById('drpSelectSourceLibrary').value = 'Seven';