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

javascript - jQuery从下拉列表中获取选定的选项(jQuery Get Selected Option From Dropdown)

Usually I use $("#id").val() to return the value of the selected option, but this time it doesn't work.

(通常,我使用$("#id").val()返回所选选项的值,但是这次不起作用。)

The selected tag has the id aioConceptName

(所选标签的ID为aioConceptName)

html code

(HTML代码)

<label>Name</label>
<input type="text" name="name" />
<select id="aioConceptName">
    <option>choose io</option>
    <option>roma</option>
    <option>totti</option>
</select>
  ask by William Kinaan translate from so

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

1 Reply

0 votes
by (71.8m points)

For dropdown options you probably want something like this:

(对于下拉选项,您可能想要这样的东西:)

var conceptName = $('#aioConceptName').find(":selected").text();

The reason val() doesn't do the trick is because clicking an option doesn't change the value of the dropdown--it just adds the :selected property to the selected option which is a child of the dropdown.

(val()不能解决问题的原因是,单击选项不会更改下拉菜单的值,它只是将:selected属性添加到作为下拉菜单的子级的selected选项中。)


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

...