国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jQuery select控制插件

2024-05-06 14:14:25
字體:
供稿:網(wǎng)友
和大家分享一下代碼:
JavaScript代碼
代碼如下:
//得到select項的個數(shù)
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();
}
}
//設(shè)置select中值為value的項為選中
jQuery.fn.setSelectedValue = function(value)
{
jQuery(this).get(0).value = value;
}
//設(shè)置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("下拉框中不存在該項");
}
}
//設(shè)置選中指定索引項
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中添加一項,顯示內(nèi)容為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中選定的項
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 宜阳县| 临夏市| 页游| 马山县| 泊头市| 独山县| 通榆县| 潮安县| 拉萨市| 肇源县| 三穗县| 香港| 合川市| 绥滨县| 晋江市| 双峰县| 图们市| 长葛市| 温州市| 扎囊县| 虎林市| 龙口市| 安康市| 九台市| 鄂托克旗| 靖西县| 于田县| 贵德县| 加查县| 宜君县| 平谷区| 丰原市| 卢湾区| 湖州市| 黄龙县| 寿宁县| 富民县| 汉阴县| 石屏县| 榆林市| 雷州市|