Home  >  Article  >  Web Front-end  >  Detailed explanation of jquery operation select (value, setting selection)_jquery

Detailed explanation of jquery operation select (value, setting selection)_jquery

WBOY
WBOYOriginal
2016-05-16 17:01:15941browse

Every time you operate a select, you always have to go out and look up the information. Why not summarize it yourself and just look here in the future.

For example

1. Select the item with value set to pxx

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

2. Select the item that sets text to pxx

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

Here is a usage of square brackets. The equal sign in the square brackets is preceded by the attribute name without 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();

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.

For example:

Copy code The code is as follows:

$ (".selector1").change(function(){

// Clear the second one first

$(".selector2").empty();

// In actual applications, multiple options here are generally generated using loops

var option = $("

$(".selector2").append(option);

});

Statement:
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