使用jQuery在dropdownlist中设置默认值 [英] Set the default value in dropdownlist using jQuery
问题描述
< option value =1>它是我< /选项>
我需要在标签内选择有价值的选项 ,而不是像 1
的属性。
我该如何使用jQuery?
如果您想使用jQuery进行此操作,请尝试以下代码。
$('select option [value =1]')。attr(selected,true);
更新:
Vivek正确地指出,史蒂芬·斯皮尔伯格(Steven Spielberg)希望通过其文本值选择该选项。
以下是更新的代码。
$('select选项:contains(it\ my 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屋!