国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS,Jquery獲取select,dropdownlist,checkbox下拉列表框的值(示例代碼)

2019-11-20 21:16:22
字體:
來源:轉載
供稿:網友

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("<option value='Value'>Text</option>");  //為Select追加一個Option(下拉項)
2. $("#select_id").prepend("<option value='0'>請選擇</option>");  //為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

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的項目為當前選中項 
                $("<option value='1'>1111</option><option value='2'>2222</option>").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{
$("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));
}
}

//取得下拉

主站蜘蛛池模板: 保定市| 黔西县| 邵阳县| 靖安县| 安多县| 邵东县| 大宁县| 台北市| 浑源县| 北安市| 新邵县| 亚东县| 云梦县| 紫阳县| 霍城县| 高清| 赞皇县| 宿迁市| 南康市| 吴桥县| 元朗区| 江门市| 连城县| 财经| 合肥市| 岑巩县| 紫云| 永定县| 淅川县| 泸西县| 鄯善县| 高平市| 闽侯县| 上虞市| 呼伦贝尔市| 周口市| 屏东县| 徐汇区| 临夏县| 曲周县| 墨玉县|