打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
jquery操作select
WindySky
>《jquery》
2011.08.21
关注
//得到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
;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
jquery的each()详细介绍
jquery 将json对象赋值给form表单
jquery 获取页面url,并根据url模拟单击事件
css(name|pro|[,val|fn]) | jQuery API 1.12 中文文档 | jQuery API 在线手册
我最喜欢的jQuery插件模板
js常用方法之事件注册
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×