使用jQuery在dropdownlist中设置默认值 [英] Set the default value in dropdownlist using jQuery

查看:144
本文介绍了使用jQuery在dropdownlist中设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的下拉列表中有很多选项,例如:

 < 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。



JSFiddle上的工作示例


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.

A working example on JSFiddle

这篇关于使用jQuery在dropdownlist中设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆