current position:Home>JQuery operation select (value, setting, selected)

JQuery operation select (value, setting, selected)

2021-08-22 05:30:24 mb611f1a641bd18

such as <select class="selector"></select>

1、 Set up value by pxx Check the item of

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

2、 Set up text by pxx Check the item of

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

    Here is a usage of brackets , The equal sign in square brackets is preceded by the attribute name , No quotes . A lot of times , The use of brackets can make logic very simple .

3、 Gets the... Of the currently selected item value

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

4、 Gets the... Of the currently selected item text

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

    The colon is used here , Mastering its usage will also make the code concise .

 

It is often used select Cascade of , That is, the second select With the first select Selected value changes . This is in jquery Is very simple .

Such as :$(".selector1").change(function(){

     // First empty the second

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

     // In practical application , there option Generally, multiple are generated by loops

      var option = $("<option>").val(1).text("pxx");

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

});

copyright notice
author[mb611f1a641bd18],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210822053018976P.html

Random recommended