使用jQuery在dropdownlist中设置默认值 [英] Set the default value in dropdownlist using jQuery
问题描述
我的下拉列表中有很多选项,例如:
< option value =1>选项>
我需要在标签中选择具有值 ,而不是像 1
这样的属性。
如何使用jQuery?
$ b $如果您想要使用jQuery,请尝试以下代码: $('select option [value =1]')。attr(selected,true);
更新时间:
Vivek,正确地指出,史蒂文·斯皮尔伯格希望通过其文本值选择选项。
以下是更新后的代码。
$选项:contains(it\'s me)')。prop('selected',true);
此外,jQuery prop 在获取和设置属性时更好地支持Internet Explorer。
I have many options in my dropdownlist like:
<option value="1">it's me</option>
I need to select the option who have value it's me inside the tag, not by attribute like 1
.
How can I do this using jQuery?
if your wanting to use jQuery for this, try the following code.
$('select option[value="1"]').attr("selected",true);
Updated:
Following a comment from Vivek, correctly pointed out steven spielberg wanted to select the option via its Text value.
Here below is the updated code.
$('select option:contains("it\'s me")').prop('selected',true);
You need to use the :contains(text)
selector to find via the containing text.
Also jQuery prop offeres better support for Internet Explorer when getting and setting attributes.
这篇关于使用jQuery在dropdownlist中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!