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
333 views
in Technique[技术] by (71.8m points)

javascript - jQuery获取特定的选项标签文本(jQuery get specific option tag text)

All right, say I have this:

(好吧,说我有这个:)

<select id='list'>
    <option value='1'>Option A</option>
    <option value='2'>Option B</option>
    <option value='3'>Option C</option>
</select>

What would the selector look like if I wanted to get "Option B" when I have the value '2'?

(如果我想获得值“ 2”时选择“选项B”,选择器会是什么样?)

Please note that this is not asking how to get the selected text value, but just any one of them, whether selected or not, depending on the value attribute.

(请注意,这不是在询问如何获取选定的文本值,而是询问其中的任何一个(取决于是否选中),具体取决于value属性。)

I tried:

(我试过了:)

$("#list[value='2']").text();

But it is not working.

(但这是行不通的。)

  ask by Paolo Bergantino translate from so

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

1 Reply

0 votes
by (71.8m points)

If you'd like to get the option with a value of 2, use

(如果您想获得值为2的选项,请使用)

$("#list option[value='2']").text();

If you'd like to get whichever option is currently selected, use

(如果要获取当前选择的任何一个选项,请使用)

$("#list option:selected").text();

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

...