語法解釋:jquery
$("#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最大的索引值
jQuery設置Select選擇的Text和Value:app
語法解釋:this
$("#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的項選中
jQuery添加/刪除Select的Option項:code
語法解釋:索引
$("#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
jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關獲取一組radio被選中項的值事件
var item = $('input[@name=items][@checked]').val();
獲取select被選中項的文本ip
var item = $("select[@name=items] option[@selected]").text();
select下拉框的第二個元素爲當前選中值rem
$('#select_id')[0].selectedIndex = 1;
radio單選組的第二個元素爲當前選中值get
$('input[@name=items]').get(1).checked = true;
獲取值:input
控制表單元素:
文本框,文本區域:
$("#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]")); } }
//取得下拉選框的選取值
$(#testSelect option:selected').text();
或 $("#testSelect").find('option:selected').text();
或 $("#testSelect").val();
複選框,推薦以下操做方式
$("#chk2")[0].checked = true;
$("#chk2")[0].checked = false;