jQuery gets the Text and Value selected by Select (detailed summary)

The Text and Value selected by Select may both need to obtain their values ​​in use, and even collect user needs. This article sorts out some common and practical operating skills. Interested friends can understand and consolidate their knowledge. , I hope this article is helpful to you

Syntax explanation:
1. $("#select_id").change(function(){//code...}); //Add an event to Select and trigger when one of them is selected
2. var checkText=$(" #select_id").find("option:selected").text(); //Get Select selected Text
3. var checkValue=$("#select_id").val(); //Get Select selected Value
4 . var checkIndex=$("#select_id ").get(0).selectedIndex; //Get the index value of Select
5. var maxIndex=$("#select_id option:last").attr("index"); //Get the largest index value of Select


jQuery sets the Text and Value of Select:
Syntax explanation:
1. $("#select_id ").get(0).selectedIndex=1; //Set the item with the Select index value to 1 to select
2. $("#select_id " ).val(4); // Set the value of Select to 4 and select
3. $("#select_id option[text='jQuery']").attr("selected", true); //Set Select The item whose Text value is jQuery is selected


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


jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related
Obtain the value of a set of radio selected items
var item = $('input[name=items][checked]' ).val();
Get the text of the selected item in select
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 single-selection group is the currently selected value
$('input[name=items]').get(1).checked = true;
Get the value:
Text box, text area: $("#txt").attr("value");
multi-select 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",'');//Empty the content
$("#txt").attr("value",'11');//Fill the content
Checkbox: $("#chk1").attr("checked",'');//Do not tick
$("#chk2").attr("checked",true);//Check
if ($("#chk1").attr('checked')==undefined) //Determine whether the
radio group radio has been checked : $("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 Currently selected item
$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//Add drop-down box option
$ ("#sel").empty();//Empty the drop-down box
---------------------------------- -------------------------------------------------- ----------------


//遍历option和添加、移除option
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{
$("<option value='111'>UPS Ground</option>").appendTo($("select[name=ISHIPTYPE]"));
}
}


//Get the selected 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 bookmark it:
1. Drop-down box:
var cc1 = $(".formc select[name='country'] option[selected]").text(); / /Get the text of the selected item in the 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('');
$("<option value='1'>1111</option>").appendTo("#select")//Add a drop-down box Option


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

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


3. Checkbox:
$("input[@type=checkbox][@checked]").val(); //Get the value of the first item selected in the checkbox
$("input[@type=checkbox ][@checked]").each(function() {//Because there are usually multiple checkboxes selected, you can cycle through the output
alert($(this).val());
});

$("# chk1").attr("checked",'');//Do not check
$("#chk2").attr("checked",true);// Check
if($("#chk1"). attr('checked')==undefined){} //Determine whether it has been ticked


当然jquery的选择器是强大的. 还有很多方法.
<script src="jquery-1.2.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#selectTest").change(function()
{
//alert("Hello");
//alert($("#selectTest").attr("name"));
//$("a").attr("href","xx.html");
//window.location.href="xx.html";
//alert($("#selectTest").val());
alert($("#selectTest option[@selected]").text());
$("#selectTest").attr("value", "2");

});
});
</script>


<a href="#">aaass</a>

<!--Dropdown box-->
<select id="selectTest" name="selectTest">
<option value="1">11</option>
<option value="2">22</option>
<option value="3">33</option>
<option value="4">44</option>
<option value="5">55</option>
<option value="6">66</option>
</select>
jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related to obtain a group of radio selected items The value
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 single-selection group is the currently selected value
$('input[@name=items] ').get(1).checked = true;
Get value:
text box, text area: $("#txt").attr("value");
multi-select box checkbox: $("#checkbox_id").attr("value");
single-select group radio: $ ("input[@type=radio][@checked]").val();
drop-down box select: $('#sel').val();
control form element:
text box, text area: $("# txt").attr("value",'');//Empty the content
$("#txt").attr("value",'11');// Fill in the content
checkbox: $("# chk1").attr("checked",'');//Do not check
$("#chk2").attr("checked",true);// Check
if($("#chk1"). attr('checked')==undefined) //Determine whether the
radio group radio has been checked : $("input[@type=radio]").attr("checked",'2');//Set value The item with =2 is the currently selected item.
Drop-down box select: $("#sel").attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item
$("<optionvalue='1'& gt;1111</option><optionvalue='2'>2222 </option& gt;").appendTo("#sel")//Add drop-down box option
$("#sel").empty();// Clear the drop-down box to

obtain the value of a selected radio item
var item = $('input[@name=items][@checked]').val();
Get the text of the selected item in select
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 single-selection group is the currently selected value
$('input[@name=items]').get(1).checked = true;
Get the value:
text box, Text area: $("#txt").attr("value");
multi-select box checkbox: $("#checkbox_id").attr("value");
single-select group radio: $("input[@type =radio][@checked]").val();
drop-down box select: $('#sel').val();
control form elements:
text box, text area: $("#txt").attr( "value",'');//Empty the content
$("#txt").attr("value",'11');// Fill in the content
Multi-select box checkbox: $("#chk1").attr("checked",'');//Do not tick
$ ( "# chk2") attr ( "checked", true);. // tick
(. $ ( "# chk1" ) attr ( 'checked') == undefined) if // check to determine whether
the radio 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
$("<option value='1'>1111</option><option value='2'> 2222</option>").appendTo("#sel")//Add the option of the drop-down box
$("#sel").empty();// Clear the drop-down box

Guess you like

Origin blog.csdn.net/zhaofuqiangmycomm/article/details/115112827