Home  >  Article  >  Web Front-end  >  jQuery gets the Text and Value selected by Select

jQuery gets the Text and Value selected by Select

巴扎黑
巴扎黑Original
2017-06-29 10:09:001179browse

jQuery gets the Text and Value selected by Select:
Syntax explanation:
1. $("#select_id").change(function(){//code...} ); //Add event to Select, triggered when one of the items is selected
2. var checkText=$("#select_id").find("option:selected").text() ; //Get the Text selected by Select
3. var checkValue=$("#select_id").val(); //Get the Value selected by Select
4. var checkIndex=$("#select_id ") .get(0).selectedIndex; //Get the indexvalue
5 selected by Select. var maxIndex=$("#select_id option:last").attr("index"); // Get the maximum index value of Select
jQuery sets the Text and Value selected by Select:
Syntax explanation:
1. $("#select_id ").get(0).selectedIndex=1; //Set Select The item with index value 1 is selected
2. $("#select_id ").val(4); // Set the Select Value to 4 and the item is selected
3. $("#select_id option[text ='jQuery']").attr("selected", true); //Set the Text value of Select to jQuery's item selection

jQuery adds/delete the Option item of Select:
Syntax explanation:
1. $("#select_id").append(""); //Append an Option (drop-down item) to Select
2. $("#select_id").prepend(""); //Insert an Option (first position) for Select
3. $ ("#select_id option:last").remove(); //Delete the Option (the last one) with the largest index value in Select
4. $("#select_id option[index='0']").remove( ); //Delete the Option (first one) with index value 0 in Select
5. $("#select_id option[value='3']").remove(); //Delete Value= in Select '3''s Option
5. $("#select_id option[text='4']").remove(); //Delete the Select's Text='4' Option

http: //www.cnblogs.com/SAL2928/archive/2008/10/28/1321285.html

jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and Its related
Get the value of a group of radio selected items
var item = $('input[name=items][checked]').val();
Get the text of the selected item
var item = $("select[name=items] option[selected]").text();
The second element of the select drop-down box is the currently selected value
$('#select_id') [0].selectedIndex = 1;
The second element of the radio radio group is the currently selected value
$('input[name=items]').get(1).checked = true;
Get the value:
Text box, text area: $("#txt").attr("value");
Multiple selection box checkbox: $("#checkbox_id").attr("value") ;
Single selection group radio: $("input[type=radio][checked]").val();
Drop-down box select: $('#sel').val();
Control form elements:
Text box, text area: $("#txt").attr("value",'');//Clear content
$("#txt").attr("value ",'11');//Fill content
Multiple selection box checkbox: $("#chk1").attr("checked",'');//Uncheck
$("#chk2 ").attr("checked",true);//Check
if($("#chk1").attr('checked')==undefined) //Determine whether it has been checked
Select group radio: $("input[type=radio]").attr("checked",'2');//Set the item with value=2 as the currently selected item
Drop-down box select: $("# sel").attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item
$(" ").appendTo("#sel")//Add the option of the drop-down box
$("#sel").empty();//Clear Drop-down box

--------------------------------------------- -------------------------------------------------- --------

//Traverse options and add and remove options
function changeShipMethod(shipping){
var len = $("select[name=ISHIPTYPE] option" ).length
if(shipping.value != "CA"){
$("select[name=ISHIPTYPE] option").each(function(){
if($(this). val() == 111){
$(this).remove();
}
});
}else{
$("").appendTo($("select[name=ISHIPTYPE]"));
}
}

//Get the selection value of the drop-down menu

$(#testSelect option:selected').text();
or $("#testSelect").find('option:selected').text();
or $("# testSelect").val();
////////////////////////////////////////// ////////////////////////////
If you have a bad memory, you can save it:
1, drop-down box:

var cc1 = $(".formc select[name='country'] option[selected]").text(); //Get the text of the selected item of drop-down menu (note that there is a space in the middle )
var cc2 = $('.formc select[name="country"]').val(); //Get the value of the selected item in the drop-down menu
var cc3 = $('.formc select[ name="country"]').attr("id"); //Get the ID attribute value of the selected item in the drop-down menu
$("#select").empty(); //Clear the drop-down box// $("#select").html('');
$("").appendTo("#select")//Add drop-down box Option

A little explanation:
1.select[name='country'] option[selected] means that it has a name attribute,
and the attribute value is in the select element of 'country' option element with selected attribute;

2, radio button:
$("input[@type=radio][@checked]").val(); //Get the radio button The value of the selected item (note that there is no space in the middle)
$("input[@type=radio][@value=2]").attr("checked",'checked'); //Set the radio button value =2 is selected. (Note that there is no space in the middle)

3,checkbox:
$("input[@type=checkbox][@checked]") .val(); //Get the value of the first selected item of the check box
$("input[@type=checkbox][@checked]").each(function() { //Due to the check Generally, multiple boxes are selected, so you can output it in a loop
alert($(this).val());
});

$("#chk1").attr(" checked",'');//Unchecked
$("#chk2").attr("checked",true);//Checked
if($("#chk1").attr ('checked')==undefined){} //Determine whether it has been checked

Of course jquery's selector is powerful. There are many methods.