Home > Web Front-end > JS Tutorial > body text

Detailed example of how jquery allows select to select an option

黄舟
Release: 2017-07-28 09:29:48
Original
4536 people have browsed it

Jquery How to let select select an option Detailed example

<span id="xx">广州市</span>
<select id="select_id">    
<option>北京市</option>    
<option>广州市</option>    
<option>上海市</option>
</select>
Copy after login

Let select select Guangzhou City?
Incorrect usage:

$("#select_id option[text=&#39;广州市&#39;]").attr("selected", true); 
<script>
//普通js写法
document.getElementById("select_id").options[1].setAttribute("selected", true);
//jquery应该这样写
$("#select_id option").eq(1).attr("selected",true);
</script>
<script>
   $("#select_id option[value=&#39;广州市&#39;]").attr("selected", true);
</script>
$("#select_id option[text=&#39;广州市&#39;]").attr("selected", "selected");
Copy after login


Correct usage:

 $("#select_id")[0].selectedIndex = 1;
Copy after login

How to set the select index in jquery and directly use the dom operation method to operate the index:

$(&#39;#selectId&#39;).get(0).selectedIndex=index;
Copy after login

Use value mode to operate index:

$(&#39;#selectId&#39;).val(&#39;selectIndexValue&#39;);
Copy after login


Extension:
Get the value of the currently selected item

$("#select_id").val();
Copy after login

Get the text of the currently selected item

$("#select_id").find("option:selected").text();
Copy after login

The above is the detailed content of Detailed example of how jquery allows select to select an option. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!