jquery select操作的日期聯(lián)動(dòng)實(shí)現(xiàn)代碼
更新時(shí)間:2009年12月06日 00:23:01 作者:
是很簡(jiǎn)單的代碼 不過(guò)我自己操作的時(shí)候才發(fā)現(xiàn)我自己還有很多不懂,要多實(shí)際應(yīng)用才發(fā)現(xiàn)問(wèn)題,哎~~
Jquery的選擇器很強(qiáng)大,對(duì)select的options對(duì)象添加的時(shí)候我找了老半天才找到
/**//*
文件名:jquery.liu.select.js
功能說(shuō)明:本js文件為jquery類(lèi)庫(kù)的一個(gè)插件,主要實(shí)現(xiàn)對(duì)select的操作.
作者:John Liu
編寫(xiě)日期:2008/03/12
*/
//得到select項(xiàng)的個(gè)數(shù)
jQuery.fn.size = function()
{
return jQuery(this).get(0).options.length;
}
//獲得選中項(xiàng)的索引
jQuery.fn.getSelectedIndex = function()
{
return jQuery(this).get(0).selectedIndex;
}
//獲得當(dāng)前選中項(xiàng)的文本
jQuery.fn.getSelectedText = function()
{
if(this.size() == 0)
{
return "下拉框中無(wú)選項(xiàng)";
}
else
{
var index = this.getSelectedIndex();
return jQuery(this).get(0).options[index].text;
}
}
//獲得當(dāng)前選中項(xiàng)的值
jQuery.fn.getSelectedValue = function()
{
if(this.size() == 0)
{
return "下拉框中無(wú)選中值";
}
else
{
return jQuery(this).val();
}
}
//設(shè)置select中值為value的項(xiàng)為選中
jQuery.fn.setSelectedValue = function(value)
{
jQuery(this).get(0).value = value;
}
//設(shè)置select中文本為text的第一項(xiàng)被選中
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("下拉框中不存在該項(xiàng)");
}
}
//設(shè)置選中指定索引項(xiàng)
jQuery.fn.setSelectedIndex = function(index)
{
var count = this.size();
if(index >= count || index < 0)
{
alert("選中項(xiàng)索引超出范圍");
}
else
{
jQuery(this).get(0).selectedIndex = index;
}
}
//判斷select項(xiàng)中是否存在值為value的項(xiàng)
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中添加一項(xiàng),顯示內(nèi)容為text,值為value,如果該項(xiàng)值已存在,則提示
jQuery.fn.addOption = function(text,value)
{
if(this.isExistItem(value))
{
alert("待添加項(xiàng)的值已存在");
}
else
{
jQuery(this).get(0).options.add(new Option(text,value));
}
}
//刪除select中值為value的項(xiàng),如果該項(xiàng)不存在,則提示
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("待刪除的項(xiàng)不存在!");
}
}
//刪除select中指定索引的項(xiàng)
jQuery.fn.removeIndex = function(index)
{
var count = this.size();
if(index >= count || index < 0)
{
alert("待刪除項(xiàng)索引超出范圍");
}
else
{
jQuery(this).get(0).remove(index);
}
}
//刪除select中選定的項(xiàng)
jQuery.fn.removeSelected = function()
{
var index = this.getSelectedIndex();
this.removeIndex(index);
}
//清除select中的所有項(xiàng)
jQuery.fn.clearAll = function()
{
jQuery(this).get(0).options.length = 0;
}
復(fù)制代碼 代碼如下:
/**//*
文件名:jquery.liu.select.js
功能說(shuō)明:本js文件為jquery類(lèi)庫(kù)的一個(gè)插件,主要實(shí)現(xiàn)對(duì)select的操作.
作者:John Liu
編寫(xiě)日期:2008/03/12
*/
//得到select項(xiàng)的個(gè)數(shù)
jQuery.fn.size = function()
{
return jQuery(this).get(0).options.length;
}
//獲得選中項(xiàng)的索引
jQuery.fn.getSelectedIndex = function()
{
return jQuery(this).get(0).selectedIndex;
}
//獲得當(dāng)前選中項(xiàng)的文本
jQuery.fn.getSelectedText = function()
{
if(this.size() == 0)
{
return "下拉框中無(wú)選項(xiàng)";
}
else
{
var index = this.getSelectedIndex();
return jQuery(this).get(0).options[index].text;
}
}
//獲得當(dāng)前選中項(xiàng)的值
jQuery.fn.getSelectedValue = function()
{
if(this.size() == 0)
{
return "下拉框中無(wú)選中值";
}
else
{
return jQuery(this).val();
}
}
//設(shè)置select中值為value的項(xiàng)為選中
jQuery.fn.setSelectedValue = function(value)
{
jQuery(this).get(0).value = value;
}
//設(shè)置select中文本為text的第一項(xiàng)被選中
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("下拉框中不存在該項(xiàng)");
}
}
//設(shè)置選中指定索引項(xiàng)
jQuery.fn.setSelectedIndex = function(index)
{
var count = this.size();
if(index >= count || index < 0)
{
alert("選中項(xiàng)索引超出范圍");
}
else
{
jQuery(this).get(0).selectedIndex = index;
}
}
//判斷select項(xiàng)中是否存在值為value的項(xiàng)
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中添加一項(xiàng),顯示內(nèi)容為text,值為value,如果該項(xiàng)值已存在,則提示
jQuery.fn.addOption = function(text,value)
{
if(this.isExistItem(value))
{
alert("待添加項(xiàng)的值已存在");
}
else
{
jQuery(this).get(0).options.add(new Option(text,value));
}
}
//刪除select中值為value的項(xiàng),如果該項(xiàng)不存在,則提示
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("待刪除的項(xiàng)不存在!");
}
}
//刪除select中指定索引的項(xiàng)
jQuery.fn.removeIndex = function(index)
{
var count = this.size();
if(index >= count || index < 0)
{
alert("待刪除項(xiàng)索引超出范圍");
}
else
{
jQuery(this).get(0).remove(index);
}
}
//刪除select中選定的項(xiàng)
jQuery.fn.removeSelected = function()
{
var index = this.getSelectedIndex();
this.removeIndex(index);
}
//清除select中的所有項(xiàng)
jQuery.fn.clearAll = function()
{
jQuery(this).get(0).options.length = 0;
}
您可能感興趣的文章:
- 基于JQuery的日期聯(lián)動(dòng)實(shí)現(xiàn)代碼
- jquery+json 通用三級(jí)聯(lián)動(dòng)下拉列表
- 簡(jiǎn)單實(shí)用jquery版三級(jí)聯(lián)動(dòng)select示例
- jquery 實(shí)現(xiàn)二級(jí)/三級(jí)/多級(jí)聯(lián)動(dòng)菜單的思路及代碼
- JQuery打造省市下拉框聯(lián)動(dòng)效果
- jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
- jQuery 聯(lián)動(dòng)日歷實(shí)現(xiàn)代碼
- 基于JQUERY的多級(jí)聯(lián)動(dòng)代碼
- Jquery實(shí)現(xiàn)無(wú)刷新DropDownList聯(lián)動(dòng)實(shí)現(xiàn)代碼
- jQuery制作簡(jiǎn)潔的多級(jí)聯(lián)動(dòng)Select下拉框
- 用Jquery實(shí)現(xiàn)多級(jí)下拉框無(wú)刷新的聯(lián)動(dòng)
- jQuery 下拉列表 二級(jí)聯(lián)動(dòng)插件分享
- jQuery實(shí)現(xiàn)日期聯(lián)動(dòng)效果實(shí)例
相關(guān)文章
jQuery簡(jiǎn)單實(shí)現(xiàn)仿京東商城的左側(cè)菜單效果代碼
這篇文章主要介紹了jQuery簡(jiǎn)單實(shí)現(xiàn)仿京東商城的左側(cè)菜單效果代碼,通過(guò)簡(jiǎn)單的jQuery鼠標(biāo)事件及元素動(dòng)態(tài)變換實(shí)現(xiàn)樣式動(dòng)態(tài)切換功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09
jQuery EasyUI常用數(shù)據(jù)驗(yàn)證匯總
這篇文章主要為大家詳細(xì)匯總了jQuery EasyUI常用數(shù)據(jù)驗(yàn)證,介紹了validatebox()提供的自定義驗(yàn)證,感興趣的小伙伴們可以參考一下2016-09-09
Jquery與Bootstrap實(shí)現(xiàn)后臺(tái)管理頁(yè)面增刪改查功能示例
本篇文章主要介紹了Jquery與Bootstrap實(shí)現(xiàn)后臺(tái)管理頁(yè)面增刪改查功能示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
讓jQuery與其他JavaScript庫(kù)并存避免沖突的方法
為了解決jQuery和其他JavaScript庫(kù)中$()函數(shù)的沖突,需要取消jQuery的$()函數(shù),下面有個(gè)不錯(cuò)的方法,感興趣的朋友可以參考下2013-12-12
jquery插件NProgress.js制作網(wǎng)頁(yè)加載進(jìn)度條
這篇文章主要介紹了jquery插件NProgress.js制作網(wǎng)頁(yè)加載進(jìn)度條的相關(guān)資料,需要的朋友可以參考下2015-06-06
jQuery實(shí)現(xiàn)對(duì)無(wú)序列表的排序功能(附demo源碼下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)對(duì)無(wú)序列表的排序功能,涉及jQuery與javascript常見(jiàn)的文本操作函數(shù)與sort排序函數(shù)的相關(guān)使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
jQuery實(shí)現(xiàn)一個(gè)簡(jiǎn)單的輪播圖
本文主要介紹了jQuery實(shí)現(xiàn)一個(gè)簡(jiǎn)單輪播圖的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
uploadify多文件上傳參數(shù)設(shè)置技巧
uploadify插件配置實(shí)用比較簡(jiǎn)單,很多開(kāi)發(fā)者都喜歡使用。但是它有個(gè)缺點(diǎn)就是剛加載的時(shí)候稍微慢了一秒左右,本文通過(guò)一段代碼實(shí)例給大家介紹uploadify多文件上傳參數(shù)設(shè)置技巧,朋友們一起學(xué)習(xí)吧2015-11-11

