jquery select

jQuery获取Select选择的Text和Value:
语法解释:
1.
Java代码<wbr><br> $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id").change(function(){//code...});<wbr><wbr> //为<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>添加事件,当选择其中一项时触发<wbr><br> 2. var checkText=$("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id").find("option:selected").<b style="color: black; background-color: rgb(153, 255, 153);">text</b>();<wbr> //获取<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>选择的<b style="color: black; background-color: rgb(153, 255, 153);">Text</b><wbr><br> 3. var checkValue=$("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id").val();<wbr> //获取<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>选择的Value<wbr><br> 4. var checkIndex=$("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id ").get(0).selectedIndex;<wbr> //获取<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>选择的索引值<wbr><br> 5. var maxIndex=$("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id option:last").attr("index");<wbr> //获取<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>最大的索引值<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

jQuery设置Select选择的<wbr><b style="color: black; background-color: rgb(153, 255, 153);">Text</b>和Value:<br> 语法解释:<br> Java代码<wbr><br> 1. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id ").get(0).selectedIndex=1;<wbr> //设置<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>索引值为1的项选中<wbr><br> 2. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id ").val(4);<wbr><wbr> // 设置<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>的Value值为4的项选中<wbr><br> 3. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id option[<b style="color: black; background-color: rgb(153, 255, 153);">text</b>='<b style="color: black; background-color: rgb(255, 255, 102);">jQuery</b>']").attr("selected", true);<wbr><wbr> //设置<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>的<b style="color: black; background-color: rgb(153, 255, 153);">Text</b>值为<b style="color: black; background-color: rgb(255, 255, 102);">jQuery</b>的项选中<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


jQuery添加/删除Select的Option项:
语法解释:
Java代码<wbr><br> 1. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id").append("</wbr>

Text");<wbr> //为<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>追加一个Option(下拉项)<wbr><br> 2. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id").prepend("请选择");<wbr> //为<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>插入一个Option(第一个位置)<wbr><br> 3. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id option:last").remove();<wbr> //删除<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>中索引值最大Option(最后一个)<wbr><br> 4. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id option[index='0']").remove();<wbr> //删除<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>中索引值为0的Option(第一个)<wbr><br> 5. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id option[value='3']").remove();<wbr> //删除<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>中Value='3'的Option<wbr><br> 5. $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id option[<b style="color: black; background-color: rgb(153, 255, 153);">text</b>='4']").remove();<wbr> //删除<b style="color: black; background-color: rgb(160, 255, 255);">Select</b>中<b style="color: black; background-color: rgb(153, 255, 153);">Text</b>='4'的Option<wbr><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> <br><b style="color: black; background-color: rgb(255, 255, 102);">jquery</b><wbr>radio取值,checkbox取值,<b style="color: black; background-color: rgb(160, 255, 255);">select</b>取值,radio选中,checkbox选中,<b style="color: black; background-color: rgb(160, 255, 255);">select</b>选中,及其相关<br> Java代码<wbr><br> 获 取一组radio被选中项的值<wbr><br> var item = $('input[name=items][checked]').val();<wbr><br> 获 取<b style="color: black; background-color: rgb(160, 255, 255);">select</b>被选中项的文本<wbr><br> var item = $("<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name=items] option[selected]").<b style="color: black; background-color: rgb(153, 255, 153);">text</b>();<wbr><br><b style="color: black; background-color: rgb(160, 255, 255);">select</b>下拉框的第二个元素为当前选中值<wbr><br> $('#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id')[0].selectedIndex = 1;<wbr><br> radio单选组的第二个元素为当前选中值<wbr><br> $('input[name=items]').get(1).checked = true;<wbr><br> 获取值:<wbr><br> 文本框,文本区域:$("#txt").attr("value");<wbr><br> 多选框 checkbox:$("#checkbox_id").attr("value");<wbr><br> 单选组radio:<wbr><wbr> $("input[type=radio][checked]").val();<wbr><br> 下拉框<b style="color: black; background-color: rgb(160, 255, 255);">select</b>: $('#sel').val();<wbr><br> 控制表单元素:<wbr><br> 文本框,文本区域:$("#txt").attr("value",'');//清空内容<wbr><br> $("#txt").attr("value",'11');//填充内容<wbr><br> 多选框checkbox: $("#chk1").attr("checked",'');//不打勾<wbr><br> $("#chk2").attr("checked",true);//打勾<wbr><br> if($("#chk1").attr('checked')==undefined) //判断是否已经打勾<wbr><br> 单选组 radio:<wbr><wbr><wbr> $("input[type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项<wbr><br> 下拉框<wbr><b style="color: black; background-color: rgb(160, 255, 255);">select</b>:<wbr><wbr> $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项<wbr><br> $("</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> 11112222").appendTo("#sel")//添加下拉框的option<wbr><br> $("#sel").empty();//清空下拉框<wbr><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> ----------------------------------------------------------------------------------------------------</p> <p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> Java代码<wbr><br> //遍历option和添加、移除option<wbr><br> function changeShipMethod(shipping){<wbr><br> var len = $("<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name=ISHIPTYPE] option").length<wbr><br> if(shipping.value != "CA"){<wbr><br> $("<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name=ISHIPTYPE] option").each(function(){<wbr><br> if($(this).val() == 111){<wbr><br> $(this).remove();<wbr><br> }<wbr><br> });<wbr><br> }else{<wbr><br> $("</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> UPS Ground").appendTo($("<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name=ISHIPTYPE]"));<wbr><br> }<wbr><br> }<wbr><br><wbr><br> //取得下拉选单的选取值<wbr><br><wbr><br> $(#testSelect option:selected').<b style="color: black; background-color: rgb(153, 255, 153);">text</b>();<wbr><br> 或$("#testSelect").find('option:selected').<b style="color: black; background-color: rgb(153, 255, 153);">text</b>();<wbr><br> 或$("#testSelect").val();<wbr><br> //<wbr><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> 记 性不好的可以收藏下:<br> 1,下拉框:<br> Java代码<wbr><br> var cc1 = $(".formc<wbr><b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name='country'] option[selected]").<b style="color: black; background-color: rgb(153, 255, 153);">text</b>(); //得到下拉菜单的选中项的文本(注意中间有空格)<wbr><br> var cc2 = $('.formc<wbr><b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name="country"]').val(); //得到下拉菜单的选中项的值<wbr><br> var cc3 = $('.formc<wbr><b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name="country"]').attr("id"); //得到下拉菜单的选中项的ID属性值<wbr><br> $("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>").empty();//清空下拉框 //$("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>").html('');<wbr><br> $("</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> 1111").appendTo("#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>")//添加下拉框的option<wbr><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> 稍微解释一下:<br> Java代码<wbr><br> 1.<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[name='country'] option[selected] 表示具有name 属性,<wbr><br> 并 且该属性值为'country' 的<b style="color: black; background-color: rgb(160, 255, 255);">select</b>元素 里面的具有selected 属性的option 元素;<wbr><br><wbr><br> 2,单选框:<wbr><br> $("input[@type=radio][@checked]").val(); //得到单选框的 选中项的值(注意中间没有空格)<wbr><br> $("input[@type=radio][@value=2]").attr("checked",'checked'); //设置单选框value=2的为选中状态.(注意中间没有空格)<wbr><br><wbr><br> 3,复选框:<wbr><br> $("input[@type=checkbox][@checked]").val(); //得到复选框的选中的第一项的值<wbr><br> $("input[@type=checkbox][@checked]").each(function() { //由于复选框一般选中的是多个,所以可以循环输出<wbr><br> alert($(this).val());<wbr><br> });<wbr><br><wbr><br> $("#chk1").attr("checked",'');//不打勾<wbr><br> $("#chk2").attr("checked",true);// 打勾<wbr><br> if($("#chk1").attr('checked')==undefined){} //判断是否已经打勾<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> 当然<b style="color: black; background-color: rgb(255, 255, 102);">jquery</b>的选择器是强大的. 还有很多方法.</p> <p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> Java代码<wbr><br><br><br><wbr><br><a href="#">aaass</a><wbr><br><wbr><br><wbr><br> &lt;&lt;b style="color: black; background-color: rgb(160, 255, 255); "&gt;select<wbr>id="selectTest" name="selectTest"&gt;<wbr><br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> 11<wbr><br> 22<wbr><br> 33<wbr><br> 44<wbr><br> 55<wbr><br> 66<wbr><br> &lt;/&lt;b style="color: black; background-color: rgb(160, 255, 255); "&gt;select&gt;<wbr><p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> <b style="color: black; background-color: rgb(255, 255, 102);">jquery</b><wbr>radio取值,checkbox取值,<b style="color: black; background-color: rgb(160, 255, 255);">select</b>取值,radio选中,checkbox选中,<b style="color: black; background-color: rgb(160, 255, 255);">select</b>选中,及其相关获取一组radio被选中 项的值<br> Java代码<wbr><br> var item = $('input[@name=items][@checked]').val();<wbr><br> 获取<b style="color: black; background-color: rgb(160, 255, 255);">select</b>被选 中项的文本<wbr><br> var item = $("<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[@name=items] option[@selected]").<b style="color: black; background-color: rgb(153, 255, 153);">text</b>();<wbr><br><b style="color: black; background-color: rgb(160, 255, 255);">select</b><wbr>下拉框的第二个元素为当前选中值<wbr><br> $('#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id')[0].selectedIndex = 1;<wbr><br> radio单选组的第二个 元素为当前选中值<wbr><br> $('input[@name=items]').get(1).checked = true;<wbr><br> 获取值:<wbr><br> 文本 框,文本区域:$("#txt").attr("value");<wbr><br> 多选框 checkbox:$("#checkbox_id").attr("value");<wbr><br> 单选组radio: $("input[@type=radio][@checked]").val();<wbr><br> 下拉框<b style="color: black; background-color: rgb(160, 255, 255);">select</b>: $('#sel').val();<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> 控 制表单元素:<br> Java代码<wbr><br> 文本框,文本区域:$("#txt").attr("value",'');//清空内容<wbr><br> $("#txt").attr("value",'11');// 填充内容<wbr><br> 多选框checkbox: $("#chk1").attr("checked",'');//不打勾<wbr><br> $("#chk2").attr("checked",true);// 打勾<wbr><br> if($("#chk1").attr('checked')==undefined) //判断是否已经打勾<wbr><br> 单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项<wbr><br> 下拉框<wbr><b style="color: black; background-color: rgb(160, 255, 255);">select</b>: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项<wbr><br> $("2222$("#sel").empty();// 清空下拉框<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> <br> 获取一组radio被选中项的值<br> Java代码<wbr><br> var item = $('input[@name=items][@checked]').val();<wbr><br> 获取<b style="color: black; background-color: rgb(160, 255, 255);">select</b>被选中项的文本<wbr><br> var item = $("<b style="color: black; background-color: rgb(160, 255, 255);">select</b>[@name=items] option[@selected]").<b style="color: black; background-color: rgb(153, 255, 153);">text</b>();<wbr><br><b style="color: black; background-color: rgb(160, 255, 255);">select</b>下拉框的第二个元素为当 前选中值<wbr><br> $('#<b style="color: black; background-color: rgb(160, 255, 255);">select</b>_id')[0].selectedIndex = 1;<wbr><br> radio单选组的第二个元素为当前选中值<wbr><br> $('input[@name=items]').get(1).checked = true;<wbr><br> 获取值:<wbr><br> 文本框,文本区域:$("#txt").attr("value");<wbr><br> 多选框 checkbox:$("#checkbox_id").attr("value");<wbr><br> 单选组radio: $("input[@type=radio][@checked]").val();<wbr><br> 下拉框<b style="color: black; background-color: rgb(160, 255, 255);">select</b>: $('#sel').val();<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p style="border-width: 0px; padding: 0px; margin: 0px 0px 8px; list-style: none; text-indent: 2em; color: rgb(51, 51, 51); font-family: 宋体; line-height: 28px; background-color: rgb(255, 255, 255);"> 控 制表单元素:<br> 文本框,文本区域:$("#txt").attr("value",'');//清空内容<br> Java代码<wbr><br> $("#txt").attr("value",'11');// 填充内容<wbr><br> 多选框checkbox: $("#chk1").attr("checked",'');//不打勾<wbr><br> $("#chk2").attr("checked",true);// 打勾<wbr><br> if($("#chk1").attr('checked')==undefined) //判断是否已经打勾<wbr><br> 单选组radio: $("input[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项<wbr><br> 下拉框<wbr><b style="color: black; background-color: rgb(160, 255, 255);">select</b>: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项<wbr><br> $("</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> 11112222").appendTo("#sel")//添加下拉框的option<wbr><br> $("#sel").empty();// 清空下拉框 <wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值