一个非常好用的jquery 操作表单下拉列表select 插件
使用的时候先引入jquery.js文件
再引入jquery.liu.select.js文件
然后就可调用该插件的方法。
说明:该插件中的方法在ie7和firefox中验证通过,有错误和需要改进的地方还希望大家批评指正。
比如,我要清除id为selEmail的下拉框中的所有项,那么我就可以这么操作:
$( "#selEmail" ).clearAll(); |
代码文件如下:
/* 文件名:jquery.xinlg.select.js 功能说明:本js文件为jquery类库的一个插件,主要实现对select的操作. 作者:XINLG fei 编写日期:2008/03/12 */ //得到select项的个数 jQuery.fn.size = function (){ return jQuery( this ).get(0).options.length; } //获得选中项的索引 jQuery.fn.getSelectedIndex = function (){ return jQuery( this ).get(0).selectedIndex; } //获得当前选中项的文本 jQuery.fn.getSelectedText = function (){ if ( this .size() == 0) return "下拉框中无选项" ; else { var index = this .getSelectedIndex(); return jQuery( this ).get(0).options[index].text; } } //获得当前选中项的值 jQuery.fn.getSelectedValue = function (){ if ( this .size() == 0) return "下拉框中无选中值" ; else return jQuery( this ).val(); } //设置select中值为value的项为选中 jQuery.fn.setSelectedValue = function (value){ jQuery( this ).get(0).value = value; } //设置select中文本为text的第一项被选中 jQuery.fn.setSelectedText = function (text) { var isExist = false ; var count = this .size(); for ( var i=0;i<count;i++) { if (jQuery( this ).get(0).options[i].text == text) { jQuery( this ).get(0).options[i].selected = true ; isExist = true ; break ; } } if (!isExist) { alert( "下拉框中不存在该项" ); } } //设置选中指定索引项 jQuery.fn.setSelectedIndex = function (index) { var count = this .size(); if (index >= count || index < 0) { alert( "选中项索引超出范围" ); } else { jQuery( this ).get(0).selectedIndex = index; } } //判断select项中是否存在值为value的项 jQuery.fn.isExistItem = function (value) { var isExist = false ; var count = this .size(); for ( var i=0;i<count;i++) { if (jQuery( this ).get(0).options[i].value == value) { isExist = true ; break ; } } return isExist; } //向select中添加一项,显示内容为text,值为value,如果该项值已存在,则提示 jQuery.fn.addOption = function (text,value) { if ( this .isExistItem(value)) { alert( "待添加项的值已存在" ); } else { jQuery( this ).get(0).options.add( new Option(text,value)); } } //删除select中值为value的项,如果该项不存在,则提示 jQuery.fn.removeItem = function (value) { if ( this .isExistItem(value)) { var count = this .size(); for ( var i=0;i<count;i++) { if (jQuery( this ).get(0).options[i].value == value) { jQuery( this ).get(0).remove(i); break ; } } } else { alert( "待删除的项不存在!" ); } } //删除select中指定索引的项 jQuery.fn.removeIndex = function (index) { var count = this .size(); if (index >= count || index < 0) { alert( "待删除项索引超出范围" ); } else { jQuery( this ).get(0).remove(index); } } //删除select中选定的项 jQuery.fn.removeSelected = function () { var index = this .getSelectedIndex(); this .removeIndex(index); } //清除select中的所有项 jQuery.fn.clearAll = function () { jQuery( this ).get(0).options.length = 0; } |
标签:
select插件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架