JQuery对表单元素的基本操作使用总结
select下拉列表onChange事件之JQuery实现:
JQuery:
$(document).ready(function(){ $("#selectMenu").bind("change",function(){ if($(this).val()=="pro1"){ $("#pro1").slideDown(); $("#pro2").slideUp(); } elseif($(this).val()=="pro2"){ $("#pro2").slideDown(); $("#pro1").slideUp(); } }); });
HTML:
<selectid="selectMenu"> <optionvalue="">Pleaseselectproductbelow</option> <optionvalue="pro1">Product1</option> <optionvalue="pro2">Product2</option> </select>
//1.jQuery对select的基本操作 $("#select_id").change(function(){//code...});//为Select添加事件,当选择其中一项时触发 varcheckValue=$("#select_id").val();//获取Select选择的Value varcheckValue=$('.formcselect[@name="country"]').val();//得到下拉菜单name=country的选中项的值 varcheckValue=$("#select_id").val().join(",");//获取select多选(multiple="true"时候)的value varcheckText=$("#select_id").find("option:selected").text();//获取Select选择的Text varcheckText=$("select[@name=country]option[@selected]").text();//获取select被选中项的文本(注意中间有空格) varcheckText=$("#select_idoption:selected").text(); varcc2=$('.formcselect[@name="country"]').val();//得到下拉菜单的选中项的值 varcc3=$('.formcselect[@name="country"]').attr("id");//得到下拉菜单的选中项的ID属性值 varcheckIndex=$("#select_id").get(0).selectedIndex;//获取Select选择的索引值 varmaxIndex=$("#select_idoption:last").attr("index");//获取Select最大的索引值 $("#select_id").get(0).selectedIndex=1;//设置Select索引值为1(第二项)的项选中 $('#select_id')[0].selectedIndex=1;//设置Select索引值为1(第二项)的项选中 $("#select_id").val(4);//设置Select的Value值为4的项选中 $("#select_idoption[text='jQuery']").attr("selected",true);//设置Select的Text值为jQuery的项选中 $("#select_id").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 $("#select_id").empty();//清空下拉框 $("#select_id").append("<optionvalue='Value'>Text</option>");//为Select追加一个Option(下拉项) $("<optionvalue='1'>1111</option><optionvalue='2'>2222</option>").appendTo("#select_id")//添加下拉框的option $("#select_id").prepend("<optionvalue='0'>请选择</option>");//为Select插入一个Option(第一个位置) $("#select_idoption:last").remove();//删除Select中索引值最大Option(最后一个) $("#select_idoption[index='0']").remove();//删除Select中索引值为0的Option(第一个) $("#select_idoption[value='3']").remove();//删除Select中Value='3'的Option $("#select_idoption[text='4']").remove();//删除Select中Text='4'的Option //2.jquery对radio的基本操作 varitem=$('input[@name=items][@checked]').val();//获取一组radio被选中项的值 varrval=$("input[@type=radio][@checked]").val();//得到单选框的选中项的值(注意中间没有空格) $('input[@name=items]').get(1).checked=true;//radio单选组的第二个元素为当前选中值 $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项 $("input[@type=radio][@value=2]").attr("checked",'checked');//设置单选框value=2的为选中状态.(注意中间没有空格) //3.jquery对checkbox的基本操作 $("#checkbox_id").attr("value");//多选框checkbox $("input[@type=checkbox][@checked]").val();//得到复选框的选中的第一项的值 $("input[@type=checkbox][@checked]").each(function(){//由于复选框一般选中的是多个,所以可以循环输出 alert($(this).val()); }); $("#chk1").attr("checked",'');//不打勾 $("#chk2").attr("checked",true);//打勾 if($("#chk1").attr('checked')==undefined)//判断是否已经打勾 //4.jquery对text的基本操作 $("#txt").attr("value");//文本框,文本区域: $("#txt").attr("value",'');//清空内容 $("#txt").attr("value",'11');//填充内容