首頁 > web前端 > js教程 > 主體

jQuery取得Select選擇的Text和 Value

巴扎黑
發布: 2017-06-29 10:09:00
原創
1179 人瀏覽過
 

    jQuery取得Select選擇的Text和Value:
語法解釋:
1. $("#select_id").change(function(){//code...} );   //為Select新增事件,當選擇其中一項時觸發
2. var checkText=$("#select_id").find("option:selected").text() ;  //取得Select選擇的Text
3. var checkValue=$("#select_id").val();  //取得Select選擇的Value
4. var checkIndex=$("#select_id ") .get(0).selectedIndex;  //取得Select選擇的索引
5. var maxIndex=$("#select_id option:last").attr("index");  //取得Select最大的索引值
jQuery設定Select選擇的Text和Value:
語法解釋:
1. $("#select_id ").get(0).selectedIndex=1;  //設定Select索引值為1的項目選取
2. $("#select_id ").val(4);   // 設定Select的Value值為4的項目選取
3. $("#select_id option[text ='jQuery']").attr("selected", true);   //設定Select的Text值為jQuery的項目選取

jQuery新增/刪除Select的Option項目:
語法解釋:
1. $("#select_id").append("");  //為Select追加一個Option(下拉項目)
2. $("#select_id").prepend("");  //為Select插入一個Option(第一個位置)
3. $ ("#select_id option:last").remove();  //刪除Select中索引值最大Option(最後一個)
4. $("#select_id option[index='0']").remove( );  //刪除Select中索引值為0的Option(第一個)
5. $("#select_id option[value='3']").remove();  //刪除Select中Value= '3'的Option
5. $("#select_id option[text='4']").remove();  //刪除Select中Text='4'的Option

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

jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關性
取得一組radio被選取項目的值
var item = $('input[name=items][checked]').val();
取得select被選取項目的文字
var item = $("select[name=items] option[selected]").text();
select下拉方塊的第二個元素為目前選取值
$('#select_id') [0].selectedIndex = 1;
radio單選組的第二個元素為目前選取值
$('input[name=items]').get(1).checked = true;
取得值:
文字框,文字區域:$("#txt").attr("value");
多重選取框checkbox:$("#checkbox_id").attr("value") ;
單選組radio:   $("input[type=radio][checked]").val();
下拉框select: $('#sel').val();
控製表單元素:
文字框,文字區域:$("#txt").attr("value",'');//清空內容
$("#txt").attr("value ",'11');//填充內容
多重選取框checkbox: $("#chk1").attr("checked",'');//不勾選
$("#chk2 ").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾
單選組radio:    $("input[type=radio]").attr("checked",'2');//設定value=2的項目為目前選取項目
下拉方塊select:   $("# sel").attr("value",'-sel3');//設定value=-sel3的項目為目前選取項目
$(" ").appendTo("#sel")//新增下拉框的option
$("#sel").empty();//清空下拉框

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

//遍歷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{
$("").appendTo($("select[name=ISHIPTYPE]"));
}
}

//取得下拉式選單的選取值

$(#testSelect option:selected').text();
或$("#testSelect").find('option:selected').text();
或$("# testSelect").val();
/////////////////////////////////////// ///////////////////////////
記性不好的可以收藏下:
1,下拉方塊:

#var cc1 = $(".formc select[name='country'] option[selected]").text(); //得到下拉選單的選取項目的文字(注意中間有空格)
var cc2 = $('.formc select[name="country"]').val(); //得到下拉式選單的選取項目的值
var cc3 = $('.formc select[ name="country"]').attr("id"); //得到下拉式選單的選取項目的ID屬性值
$("#select").empty();//清空下拉方塊// $("#select").html('');
$("").appendTo("#select")//新增下拉框的option

稍微解釋一下:
1.select[name='country'] option[selected] 表示具有name 屬性,
並且該屬性值為'country' 的select元素裡面的具有selected 屬性的option 元素;

2,單選框:
$("input[@type=radio][@checked]").val(); //得到單選框的選取項目的值(注意中間沒有空格)
$("input[@type=radio][@value=2]").attr("checked",'checked'); //設定單選框value =2的為選取狀態.(注意中間沒有空格)

3,複選框:
$("input[@type=checkbox][@checked]") .val(); //得到複選框的選取的第一項的值
$("input[@type=checkbox][@checked]").each(function() { //由於複選框一般選取的是多個,所以可以循環輸出
alert($(this).val());
});

$("#chk1").attr(" checked",'');//不勾選
$("#chk2").attr("checked",true);// 勾選
if($("#chk1").attr ('checked')==undefined){} //判斷是否已經打勾

當然jquery的選擇器是強大的. 還有很多方法.


熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!