Home > Article > Web Front-end > jQuery dynamically adds and deletes select items (implementation code)_jquery
//Get the select value
var text=$("#mySelect").find("option:selected").text(); //Get the Text of the Select item
var value=$ ("#mySelect").val(); //Get the Value of the Select item
var value=$("#mySelect option:selected").attr("value"); //Get the Value of the Select item Value
var index=$("#mySelect").get(0).selectedIndex; //Get the index value of the Select option, starting from 0
var index=$("#mySelect option:selected" ).attr("index"); //Not available! ! !
var index=$("#mySelect option:selected").index(); //Get the index value of the Select option, starting from 0
var maxIndex=$("#mySelect option:last") .attr("index"); //Not available! ! !
var maxIndex=$("#mySelect option:last").index();//Get the Select maximum index value, starting from 0
$("#mySelect").prepend(""); //Insert an item before the first item in Select
//Set the select value
//Set the selected item according to the index
$("#mySelect").get(0).selectedIndex=index;//index is the index value
//According to value sets the selected item
$("#mySelect").attr("value","newValue");
$("#mySelect").val("newValue");
$(" #mySelect").get(0).value = value;
//Set the corresponding item as the selected item according to the text
var count=$("#mySelect option").length;
for( var i=0;i
if($("#mySelect").get(0).options[i].text == text)
{
$("#mySelect").get(0).options[i].selected = true;
break;
}
}
// Clear selection
$("#mySelect").empty();