JQuery對(duì)表單元素的基本操作使用總結(jié)
更新時(shí)間:2014年07月18日 11:38:58 投稿:whsnow
這篇文章主要介紹了JQuery對(duì)表單元素的基本操作使用總結(jié),需要的朋友可以參考下
select下拉列表onChange事件之JQuery實(shí)現(xiàn):
JQuery:
$(document).ready(function () { $("#selectMenu").bind("change", function () { if ($(this).val() == "pro1") { $("#pro1").slideDown(); $("#pro2").slideUp(); } else if($(this).val() =="pro2") { $("#pro2").slideDown(); $("#pro1").slideUp(); } }); });
HTML:
<select id="selectMenu"> <option value="" >Please select product below</option> <option value="pro1">Product 1</option> <option value="pro2">Product 2</option> </select>
//1.jQuery對(duì)select的基本操作 $("#select_id").change(function(){ //code...}); //為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā) var checkValue=$("#select_id").val(); //獲取Select選擇的Value var checkValue = $('.formc select[@name="country"]').val(); //得到下拉菜單name=country的選中項(xiàng)的值 var checkValue=$("#select_id").val().join(","); //獲取select多選(multiple="true"時(shí)候) 的value var checkText = $("#select_id").find("option:selected").text(); //獲取Select選擇的Text var checkText = $("select[@name=country] option[@selected]").text(); //獲取select被選中項(xiàng)的文本(注意中間有空格) var checkText = $("#select_id option:selected").text(); var cc2 = $('.formc select[@name="country"]').val(); //得到下拉菜單的選中項(xiàng)的值 var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜單的選中項(xiàng)的ID屬性值 var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值 var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值 $("#select_id ").get(0).selectedIndex = 1; //設(shè)置Select索引值為1(第二項(xiàng))的項(xiàng)選中 $('#select_id')[0].selectedIndex = 1; //設(shè)置Select索引值為1(第二項(xiàng))的項(xiàng)選中 $("#select_id ").val(4); //設(shè)置Select的Value值為4的項(xiàng)選中 $("#select_id option[text='jQuery']").attr("selected", true); //設(shè)置Select的Text值為jQuery的項(xiàng)選中 $("#select_id").attr("value",'-sel3'); //設(shè)置value=-sel3的項(xiàng)目為當(dāng)前選中項(xiàng) $("#select_id").empty(); //清空下拉框 $("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個(gè)Option(下拉項(xiàng)) $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#select_id")//添加下拉框的option $("#select_id").prepend("<option value='0'>請(qǐng)選擇</option>"); //為Select插入一個(gè)Option(第一個(gè)位置) $("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個(gè)) $("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個(gè)) $("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option $("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option //2.jquery對(duì)radio的基本操作 var item = $('input[@name=items][@checked]').val(); //獲取一組radio被選中項(xiàng)的值 var rval = $("input[@type=radio][@checked]").val(); //得到單選框的選中項(xiàng)的值(注意中間沒有空格) $('input[@name=items]').get(1).checked = true; //radio單選組的第二個(gè)元素為當(dāng)前選中值 $("input[@type=radio]").attr("checked",'2'); //設(shè)置value=2的項(xiàng)目為當(dāng)前選中項(xiàng) $("input[@type=radio][@value=2]").attr("checked",'checked'); //設(shè)置單選框value=2的為選中狀態(tài).(注意中間沒有空格) //3.jquery對(duì)checkbox的基本操作 $("#checkbox_id").attr("value"); //多選框checkbox $("input[@type=checkbox][@checked]").val(); //得到復(fù)選框的選中的第一項(xiàng)的值 $("input[@type=checkbox][@checked]").each(function(){ //由于復(fù)選框一般選中的是多個(gè),所以可以循環(huán)輸出 alert($(this).val()); }); $("#chk1").attr("checked",'');//不打勾 $("#chk2").attr("checked",true);//打勾 if($("#chk1").attr('checked')==undefined) //判斷是否已經(jīng)打勾 //4.jquery對(duì)text的基本操作 $("#txt").attr("value"); //文本框,文本區(qū)域: $("#txt").attr("value",''); //清空內(nèi)容 $("#txt").attr("value",'11');//填充內(nèi)容
相關(guān)文章
jquery實(shí)現(xiàn)美觀的導(dǎo)航菜單鼠標(biāo)提示特效代碼
這篇文章主要介紹了jquery實(shí)現(xiàn)美觀的導(dǎo)航菜單鼠標(biāo)提示特效代碼,涉及jquery鼠標(biāo)事件及頁面animate動(dòng)畫的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09jQuery實(shí)現(xiàn)模仿微博下拉滾動(dòng)條加載數(shù)據(jù)效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)模仿微博下拉滾動(dòng)條加載數(shù)據(jù)效果,涉及jQuery響應(yīng)下拉滾動(dòng)事件動(dòng)態(tài)操作頁面元素的技巧,需要的朋友可以參考下2015-12-12jQuery基于cookie實(shí)現(xiàn)的購物車實(shí)例分析
這篇文章主要介紹了jQuery基于cookie實(shí)現(xiàn)購物車的方法,結(jié)合實(shí)例形式簡單分析了jQuery基于cookie實(shí)現(xiàn)針對(duì)商品信息的購物車存儲(chǔ)功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12jquery 表單進(jìn)行客戶端驗(yàn)證demo
用jquery對(duì)表單進(jìn)行客戶端驗(yàn)證demo ,學(xué)習(xí)jquery的朋友可以參考下。2009-08-08