jQuery select common operations

//Find the select element and traverse.
$("select").each(function(){
      
       //Clear all option options of the current select element.
       $(this).empty();
    
       //Insert an option from the front
       $(this).prepend("<option value='first'>first option</option>");

        // Append an option from the end
       $(this).append("<option value='last'>last option</option>");

       //Set the element with value="last" to the selected state.
       $(this).val('last');

       //Get the currently selected option
       var selectOption = $(this).find("option:selected");

       //Get the value of the currently selected option
       var selectOptionValue = $(this).val();

        //Get the text of the currently selected option
       var selectOptionText = $(this).find("option:selected").text();

         //Set the option of text="pxx" to the selected state.
        $(this).find("option[text='pxx']").attr("selected",true);
});

//Multiple select elements linkage
$(".selector1").change(function(){
     // clear the second one first
      $(".selector2").empty();
     // In practical applications, the options here are generally generated by looping
     var option = "<option value='first'>first option</option>";
      $(".selector2").append(option);
});

//Delete the Option with the largest index value in the Select (the last one)
$("#select_id option:last").remove();  

 //Delete the Option with index value 0 in Select (the first one)
$("#select_id option[index='0']").remove();

 //Delete Option with Value='3' in Select
$("#select_id option[value='3']").remove();

//Delete Option with Text='4' in Select
$("#select_id option[text='4']").remove();  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326113929&siteId=291194637