Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

jquery - 按值设置选择选项“已选择”(Set select option 'selected', by value)

I have a select field with some options in it.

(我有一个select字段,其中包含一些选项。)

Now I need to select one of those options with jQuery.

(现在我需要使用jQuery选择其中一个options 。)

But how can I do that when I only know the value of the option that must be selected?

(但是当我只知道必须选择的optionvalue时,我怎么能这样做呢?)

I have the following HTML:

(我有以下HTML:)

<div class="id_100">
  <select>
    <option value="val1">Val 1</option>
    <option value="val2">Val 2</option>
    <option value="val3">Val 3</option>
  </select>
</div>

I need to select the option with value val2 .

(我需要选择值为val2的选项。)

How can this be done?

(如何才能做到这一点?)

Here's a demo page: http://jsfiddle.net/9Stxb/

(这是一个演示页面: http//jsfiddle.net/9Stxb/)

  ask by w00 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's an easier way that doesn't require you to go into the options tag:

(有一种更简单的方法,不需要你进入options标签:)

$("div.id_100 select").val("val2");

Check out the this jQuery method .

(看看这个jQuery方法 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...