A trigger event
- $(“#select_id”).change(function(){//code…}); //Add an event to Select, triggered when one of the items is selected
2. jQuery gets the selected item
1. Get the selected item directly
$(“#select_id”).find(“option:selected”)
2. Get the index of the selected item
$(“#select_id “).get(0).selectedIndex
3. Set the value as the index to get the index of the selected item
(“#select_id”).val() )
3. Get the Text and Value selected by Select
var checkText=$(“#select_id”).find(“option:selected”).text(); //Get the Text selected by Select
var checkValue=$(“#select_id”).val(); //Get the Value selected by Select
var checkIndex=$(“#select_id “).get(0).selectedIndex; //Get the index value selected by Select
var maxIndex=$(“#select_id option:last”).attr(“index”); //Get the maximum index value of Select
Fourth, set the selected item of Select through index, value and text
- index index
$(“#select_id “).get(0).selectedIndex=1; //Set the item with the Select index value of 1 to be selected
2.value
$(“#select_id “).val(4); // Set the value of Select to 4 to select the item
- text content
$("#select_id option[text='jQuery']").attr("selected", true); //Set Select's Text value to jQuery's item selection
Five, add/delete the Option item of Select
$("#select_id").append("Text"); //Append an Option (drop-down item) to Select
$("#select_id").prepend("Please select"); //Insert an Option for Select (the first position)
$(“#select_id option:last”).remove(); //Remove the Option with the largest index value in the Select (the last one)
$("#select_id option[index='0']").remove(); //Remove Option (the first) with index value 0 in Select
$("#select_id option[value='3']").remove(); //Remove Option with Value='3' in Select
$("#select_id option[text='4']").remove(); //Remove Option with Text='4' in Select
6. Other operations (radio, checkbox, select)
1. Get the value of a set of radio selected items
var item = $(‘input[@name=items][@checked]’).val();
2. Get the text of the selected item
var item = $(“select[@name=items] option[@selected]”).text();
3. The second element of the select drop-down box is the currently selected value
$(‘#select_id’)[0].selectedIndex = 1;
4. The second element of the radio radio group is the currently selected value
$(‘input[@name=items]’).get(1).checked = true;
5. Text box content settings
$(“#txt”).attr(“value”);
$("#txt").attr("value",'11');//fill content
$(“#txt”).val(“11”);
$(“#txt”).html(“11”);
6. Empty the content of the text box
$(“#txt”).attr(“value”,”);
7. Multi-select box checkbox selected
("#chk2").attr("checked",true);//Check
if($(“#chk1”).attr('checked')==undefined) //Determine whether it has been ticked
8. Radio selection of radio group
$("input[@type=radio]").attr("checked",'2');//Set the item with value=2 as the currently selected item
9. Select the drop-down box to select, add, and clear
$("#sel").attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item
$("11112222").appendTo("#sel")//Add the option of the drop-down box
$("#sel").empty();//Empty the drop-down box