jQuery对select的基本操作

来源:互联网 发布:linux ftp下载速度慢 编辑:程序博客网 时间:2024/06/07 08:06
一、基本操作

为select添加事件,当选择其中一项时触发
$("#select_id").change(function(){ });   


获取select选择的Text
var checkText=$("#select_id").find("option:selected").text();


获取Select选择的Value
var checkValue=$("#select_id").val();


获取select选择的索引值
var checkIndex=$("#select_id ").get(0).selectedIndex;  


获取select最大的索引值
var maxIndex=$("#select_id option:last").attr("index");  


设置Select索引值为1的项选中
$("#select_id ").get(0).selectedIndex=1;


设置Select的Value值为4的项选中
$("#select_id ").val(4);


设置Select的Text值为jQuery的项选中
$("#select_id option[text='jQuery']").attr("selected", true);



二、添加/删除select元素的Option项

为select追加一个Option(下拉项)
$("#select_id").append("
Text");


为select插入一个Option(第一个位置)
$("#select_id").prepend("
请选择");


删除Select中索引值最大Option(最后一个)
$("#select_id option:last").remove();


删除Select中索引值为0的Option(第一个)
$("#select_id option[index='0']").remove();


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


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


原帖地址:http://blog.sina.com.cn/s/blog_657332580100znjq.html