jQuery gets the Text and Value selected by Select:
Syntax Explanation:
1. $("#select_id").change(function(){//code...}); Add an event to the Select, which is triggered when one of the selections
2. var checkText=$("#select_id").find("option:selected").text(); Get the selected Text with Select
3. var checkValue=$("#select_id").val(); Gets the Value selected by Select
4. var checkIndex=$("#select_id ").get(0).selectedIndex; Get the index value selected by Select
5. var maxIndex=$("#select_id option:last").attr("index"); Get the largest index value for Select
jQuery sets the Text and Value selected by Select:
Syntax Explanation:
1. $("#select_id ").get(0).selectedIndex=1; Set Select to select items with an index value of 1
2. $("#select_id ").val(4); Set the Value value of Select to 4 for the item selected
3. $("#select_id option[text='jQuery']").attr("selected", true); Set the Text value of Select to jQuery
jQuery adds/removes Option items for Select:
Syntax Explanation:
1. $("#select_id").append("<option value='Value'>Text</option>"); Add an Option to Select
2. $("#select_id").prepend("<option value='0'> please select</option> "); Insert an Option (first position) for Select
3. $("#select_id option:last").remove(); Delete the Option with the largest index value in Select (last)
4. $("#select_id option[index='0']").remove(); Remove the index value of 0 in Select Option (the first one)
5. $("#select_id option[value='3']").remove(); Delete the option with value='3' in Select
5. $("#select_id option[text='4']").remove(); Delete the Option with Text='4' in Select
http://www.cnblogs.com/SAL2928/archive/2008/10/28/1321285.html
jquery radio value, checkbox value, select value, radio select, checkbox check, select select, and so on
Gets the values of a set of radio selected items
var item = $('input[@name=items][@checked]').val();
Gets 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 values:
Text box, text area: $("#txt").attr("value");
multi-check box:$("#checkbox_id").attr("value");
radio: $("input[@type=radio][@checked]").val();
drop-down select: $('#sel').val();
Control form elements:
Text box, text area: $("#txt").attr("value",''); Empty the contents
$("#txt").attr("value",'11'); Fill in the contents
checkbox: $("#chk1").attr("checked",''); No ticks
$("#chk2").attr("checked",true); Tick
if($("#chk1").attr('checked')==undefined) // Determine if it has been ticked
radio: $("input[@type=radio]").attr("checked",'2'); The item with value=2 is the currently selected item
drop-down select: $("#sel").attr("value",'-sel3'); Set value=-sel3 to the currently selected item
$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//Add the option of the dropdown box
$("#sel").empty(); Clear 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]"));
}
}
//取得下拉選單的選取值
$(#testSelect option:selected').text();
or $("#testSelect").find('option:selected').text();
or $("#testSelect").val();
//////////////////////////////////////////////////////////////////
If you have a bad memory, you can collect 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 the 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(); Empty the drop-down box //$("#select").html('');
$("<option value='1'>1111</option>").appendTo("#select")//Add the option of the dropdown box
To explain a little:
1.select[@name='country'] option[@selected] means that it has a name attribute,
And the property value is the option element with the selected attribute in the select element of 'country';
It can be seen that those that start with @ mean that they are followed by attributes.
2. Radio box:
$("input[@type=radio][@checked]").val(); Gets 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 box value=2 to the selected state. (Note that there is no space in the middle)
3. Checkbox:
$("input[@type=checkbox][@checked]").val(); Gets the value of the first item checked in the checkbox
$("input[@type=checkbox][@checked]").each(function(){ // Since the checkbox is usually checked for multiple checkboxes, it can be looped out
alert($(this).val());
});
$("#chk1").attr("checked",''); No ticks
$("#chk2").attr("checked",true); Tick
if($("#chk1").attr('checked')==undefined){} //Determine if it has been checked
Of course, jquery's selector is powerful. There are many more ways.
<scrip remove t src="jquery-1.2.1.js" type="text/javascrip remove t"></scrip去掉t>
<scrip remove t language="javascrip remove t" type="text/javascrip remove t" >
$(document).ready(function(){
$("#selectTest").change(function()
{
//alert("Hello");
//alert($("#selectTest").attr("name"));
//$("a").attr("href","xx.html");
window.locatio removes n.href="xx.html";
//alert($("#selectTest").val());
alert($("#selectTest option[@selected]").text());
$("#selectTest").attr("value", "2");
});
});
</scrip去掉t>. .
<a href="#">aaass</a>
<!-- drop-down 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, checkbox, select, radio, checkbox, select, and related to get the value of a set of radio selected items
var item = $('input[@name=items][@checked]').val();
Gets 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 values:
Text box, text area: $("#txt").attr("value");
multi-check box:$("#checkbox_id").attr("value");
radio: $("input[@type=radio][@checked]").val();
drop-down select: $('#sel').val();
Control form elements:
Text box, text area: $("#txt").attr("value",''); Empty the contents
$("#txt").attr("value",'11'); Fill in the contents
checkbox: $("#chk1").attr("checked",''); No ticks
$("#chk2").attr("checked",true); Tick
if($("#chk1").attr('checked')==undefined) // Determine if it has been ticked
radio: $("input[@type=radio]").attr("checked",'2'); The item with value=2 is the currently selected item
drop-down select: $("#sel").attr("value",'-sel3'); Set value=-sel3 to the currently selected item
$("<optionvalue='1'& gt; 1111</option><optionvalue='2'>2222</option& gt; appendTo("#sel")//Add the option of the dropdown box
$("#sel").empty(); Clear the drop-down box
Gets the values of a set of radio selected items
var item = $('input[@name=items][@checked]').val();
Gets 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 values:
Text box, text area: $("#txt").attr("value");
multi-check box:$("#checkbox_id").attr("value");
radio: $("input[@type=radio][@checked]").val();
drop-down select: $('#sel').val();
Control form elements:
Text box, text area: $("#txt").attr("value",''); Empty the contents
$("#txt").attr("value",'11'); Fill in the contents
checkbox: $("#chk1").attr("checked",''); No ticks
$("#chk2").attr("checked",true); Tick
if($("#chk1").attr('checked')==undefined) // Determine if it has been ticked
radio: $("input[@type=radio]").attr("checked",'2'); The item with value=2 is the currently selected item
drop-down select: $("#sel").attr("value",'-sel3'); Set value=-sel3 to the currently selected item
$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//Add the option of the dropdown box
$("#sel").empty(); Clear the drop-down box
|