Jquery操作radio,checkbox,select表單操作實現代碼

一 、Select  


02 jQuery獲取Select選擇的Text和Value:  


03 1. $("#select_id").change(function(){//code...}); //爲Select添加事件,當選擇其中一項時觸發  


04 2. var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的Text  


05 3. var checkValue=$("#select_id").val(); //獲取Select選擇的Value  


06 4. var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值  


07 5. var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值  


08    


09 jQuery設置Select選擇的Text和Value:  


10 1. $("#select_id ").get(0).selectedIndex=1; //設置Select索引值爲1的項選中  


11 2. $("#select_id ").val(4); //設置Select的Value值爲4的項選中  


12 3. $("#select_id option[text='jQuery']").attr("selected", true); //設置Select的Text值爲jQuery的項選中  


13    


14 jQuery添加/刪除Select的Option項:  


15 1. $("#select_id").append("<option value='Value'>Text</option>"); //爲Select追加一個Option(下拉項)  


16 2. $("#select_id").prepend("<option value='0'>請選擇</option>"); //爲Select插入一個Option(第一個位置)  


17 3. $("#select_id option:last").remove(); //刪除Select中索引值最大Option(最後一個)  


18 4. $("#select_id option[index='0']").remove(); //刪除Select中索引值爲0的Option(第一個)  


19 5. $("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option  


20 6. $("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option  


21 7. $("#SelectID").remove(); //刪除所有項  


22    


23 二、Checkbox  


24 全選/取消  


25 jQuery.attr 獲取/設置對象的屬性值,如:  


26 $("input[name='chk_list']").attr("checked"); //讀取所有name爲'chk_list'對象的狀態(是否選中)  


27 $("input[name='chk_list']").attr("checked",true); //設置所有name爲'chk_list'對象的checked爲true  


28 $("#img_1").attr("src","test.jpg"); //設置ID爲img_1的<img>src的值爲'test.jpg'  


29 $("#img_1").attr("src"); //讀取ID爲img_1的<img>src值  


30 下面的代碼是獲取上面實例中選中的checkbox的value值:  


31 <script type="text/javascript">  


32 var arrChk=$("input[name='chk_list'][checked]");  


33 $(arrChk).each(function(){  


34 window.alert(this.value);  


35 });  


36 });  


37 </script>  


38    


39 1,獲取checkbox的value  


40 $("#checkboxID").value或$("input[type='checkbox']").eq(n).attr("checked").value  


41 2,設置選中項  


42 $("input[type='checkbox']").eq(1).attr("checked")//設置第一個checkbox爲選中的項  


43 3,刪除所有checkbox  


44 $("input[type='checkbox']").remove()  


45 4,checkbox方法  


46 $(document).ready(function() {  


47 var check = $("input[type='checkbox']");  


48 check.each(function(n) {  


49 check.eq(n).bind("click", function() {  


50 if (check.eq(n).attr("checked") != false) {  


51 var value = check.eq(n).val();  


52 alert(value);  


53 }  


54 else {  


55 alert(check.eq(n).attr("checked"));  


56 }  


57 })  


58 });  


59 });  


60    


61 三、radio  


62 1,獲取選中的value值  


63 $("input[type='radio']:checked").val();  


64 2,設置指定的項爲當前選中項  


65 $("input[type='radio']").eq(1).attr("checked", true);//設置第二項爲選中項  


66 $("input[type='radio'][value='值']").attr("checked, true");  


67    


68 3,解決多個Radio  


69    


70 $("input[type='radio'][@name='rdoTest2']").eq(0).attr("checked", true);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章