zoukankan
html css js c++ java
写了个操作select的jquery插件【转】
文件名:jquery.liu.select.js
功能说明:本js文件为jquery类库的一个插件,主要实现对select的操作.
作者:John Liu
编写日期: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
;
}
使用的时候先引入jquery.js文件,再引入jquery.liu.select.js文件,然后就可调用该插件的方法。比如,我要清除id为selEmail的下拉框中的所有项,那么我就可以这么操作:$("#selEmail").clearAll();
说明:该插件中的方法在ie7和firefox中验证通过,有错误和需要改进的地方还希望大家批评指正。
查看全文
相关阅读:
好系统重装助手重装电脑系统步骤
U盘加载速度慢的解决方法
Win10应用商店缓存信息多如何去清理?
怎么消除文件左上角的白色小框?
U盘被识别但不显示盘符怎么样才能解决?
【Gamma】Scrum Meeting 2
【技术博客】 关于laravel5.1中文件上传测试的若干尝试
【Beta】Phylab 发布说明
【Beta】Phylab 测试报告
【Beta】Scrum Meeting 10
原文地址:https://www.cnblogs.com/cxd4321/p/1574750.html
最新文章
Python 爬取煎蛋网妹子图片
MSF魔鬼训练营-3.1.2信息收集-通过搜索引擎进行信息搜集
MSF魔鬼训练营-3.1.1信息收集-通过DNS和IP地址挖掘目标网络信息
在kali2.0中使用msf图形界面可能会遇到的问题
MSF魔鬼训练营第一章 初识Metasploit
BadUsb简单用法示例
CentOS 7安装docker
git命令相关
Jenkins项目构建后自动发钉钉通知
jmeter接口测试CI建设(一):服务器上安装Jenkins
热门文章
JSON parse error: Unexpected character ('a' (code 97))
struts框架之环境搭建(一)
u盘启动盘安装centos7.5操作系统
AD域环境取消密码复杂度和密码使用期限
AD域环境搭建
Autoit安装及启动
怎么提高U盘传输文件的性能?
Win7系统开机速度慢怎么解决?
怎么处理Win7系统备份还原提示代码0x80042302的错误?
如何解决Win10系统更新显示0x80080300代码的错误?
Copyright © 2011-2022 走看看