Jquery operation select details of the value of set selected

  • 2020-03-30 01:39:22
  • OfStack

Every time the operation select, always want to come out to turn over the data, as their own summary, after turning over here.

Such as < Select the class = "selector" > < / select>

1. Select the item with value set to PXX

  $(" selector "). Val (" PXX ");

2. Set text to PXX

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

There is a use of brackets, in which the equal sign is preceded by the property name, without quotation marks. In many cases, the use of brackets makes the logic simple.

3. Get the value of the currently selected item

$(" selector "). Val ();

4. Gets the text of the currently selected item

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

The colon is used here. Knowing how to use it and drawing inferences from it will also make the code cleaner.

A lot of times you use a cascade of select, where the value of the second select changes as the value of the first select is selected. This is very simple in jquery.

Such as:


$(".selector1").change(function(){
 //So let's clear the second one
  $(".selector2").empty();
 //In practice, this option usually generates multiple options in a loop
  var option = $("<option>").val(1).text("pxx");
  $(".selector2").append(option);
});


Related articles: