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

jquery operation select (value, set selected)

巴扎黑
Release: 2017-06-29 10:04:45
Original
894 people have browsed it

Every time I operate a select, I always have to go out and look up the information. Why not summarize it myself, and I will look here later.

For example

1. Set the value to pxx to select the item

$(".selector") .val("pxx");

2. Set text to pxx and select the item

$(".selector").find("option[text='pxx']") .attr("selected",true);

There is a usage of square brackets. The equal sign in the square brackets is preceded by attribute name, no need to add quotation marks. Many times, the use of square brackets can make logic very simple.

3. Get the value of the currently selected item

$(".selector").val();

4. Get the text of the currently selected item

$(".selector").find("option:selected").text();

The colon is used here. Mastering its usage and drawing inferences will also make the code simpler.

The cascade of selects is often used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.

Such as:

$(".selector1").change(function(){
     // 先清空第二个
      $(".selector2").empty();
     // 实际的应用中,这里的option一般都是用循环生成多个了
      var option = $("<option>").val(1).text("pxx");
      $(".selector2").append(option);
});
Copy after login

The above is the detailed content of jquery operation select (value, set selected). 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!