1. $("#select_id").change(function(){//code...}); //爲Select添加事件,當選擇其中一項時觸發 2. var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的 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的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'的Optiona 5. $("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Optiona 內容清空:$("#charCity").empty();
$("#select_id").change(function(){//code...}); //爲Select添加事件,當選擇其中一項時觸發 var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的text var checkValue=$("#select_id").val(); //獲取Select選擇的Value var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值 var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值
好比<select class="selector"></select>javascript
$(".selector").val("pxx");
$(".selector").find("option[text='pxx']").attr("selected",true);
這裏有一箇中括號的用法,中括號裏的等號的前面是屬性名稱,不用加引號。不少時候,中括號的運用能夠使得邏輯變得很簡單。java
$(".selector").val();
$(".selector").find("option:selected").text();
這裏用到了冒號,掌握它的用法並觸類旁通也會讓代碼變得簡潔。jquery
不少時候用到select的級聯,即第二個select的值隨着第一個select選中的值變化。這在jquery中是很是簡單的。app
如:spa
$(".selector1").change(function(){ // 先清空第二個 $(".selector2").empty(); // 實際的應用中,這裏的option通常都是用循環生成多個了 var option = $("<option>").val(1).text("pxx"); $(".selector2").append(option); });
語法解釋:code
$("#select_id").change(function(){//code...}); //爲Select添加事件,當選擇其中一項時觸發 var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的Text var checkValue=$("#select_id").val(); //獲取Select選擇的Value var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值 var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值
語法解釋:索引
$("#select_id ").get(0).selectedIndex=1; //設置Select索引值爲1的項選中 $("#select_id ").val(4); // 設置Select的Value值爲4的項選中 $("#select_id option[text='jQuery']").attr("selected", true); //設置Select的Text值爲jQuery的項選中
語法解釋:事件
$("#select_id").append("<option value='Value'>Text</option>"); //爲Select追加一個Option(下拉項) $("#select_id").prepend("<option value='0'>請選擇</option>"); //爲Select插入一個Option(第一個位置) $("#select_id option:last").remove(); //刪除Select中索引值最大Option(最後一個) $("#select_id option[index='0']").remove(); //刪除Select中索引值爲0的Option(第一個) $("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option $("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
var item = $('input[name=items][checked]').val();
var item = $("select[name=items] option[selected]").text();
$('#select_id')[0].selectedIndex = 1;
$('input[name=items]').get(1).checked = true;
$("#txt").attr("value");
$("#checkbox_id").attr("value");
$("input[type=radio][checked]").val();
$('#sel').val();
$("#txt").attr("value",'');//清空內容 $("#txt").attr("value",'11');//填充內容
$("#chk1").attr("checked",'');//不打勾 $("#chk2").attr("checked",true);//打勾 if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾
$("input[type=radio]").attr("checked",'2');//設置value=2的項目爲當前選中項
$("#sel").attr("value",'-sel3');//設置value=-sel3的項目爲當前選中項 $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option $("#sel").empty();//清空下拉框