jquery操作select下拉常用代码:
能力越大责任越大,已经是大家耳熟能详的名言了,意思谁说如果哪个人比较有能力,那么它承担的责任也就多,在网页中也是如此,select下拉菜单就是能力较强的表单元素之一,所以select被使用的频率就高,当然作为程序员的我们就要熟悉对它的操作,下面就分享一下jquery对象select下拉菜单的常用操作。
一.删除和添加option项:
$("#sel").append("<option value='Value'>Text</option>");//为select追加一个Option(下拉项) $("#sel").prepend("<option value='0'>请选择</option>"); //为select插入一个Option(第一个位置) $("#sel option:last").remove();//删除select中索引值最大Option(最后一个) $("#sel option[index='0']").remove();//删除select中索引值为0的Option(第一个) $("#sel option[value='3']").remove();//删除select中Value='3'的Option $("#sel option[text='4']").remove();//删除select中Text='4'的Option $("#sel").empty();//清空select中的option
二.获取select选择的Text和Value:
var checkText=$("#sel").find("option:selected").text();//获取select选择的Text var checkValue=$("#sel").val();//获取select选择的Value var checkIndex=$("#sel ").get(0).selectedIndex;//获取select选择的索引值 var maxIndex=$("#sel option:last").attr("index");//获取select最大的索引值
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=11467
更多内容可以参阅:http://www.softwhy.com/jquery/