jquery select操作的日期聯(lián)動實現(xiàn)代碼
更新時間:2009年12月06日 00:23:01 作者:
是很簡單的代碼 不過我自己操作的時候才發(fā)現(xiàn)我自己還有很多不懂,要多實際應(yīng)用才發(fā)現(xiàn)問題,哎~~
Jquery的選擇器很強大,對select的options對象添加的時候我找了老半天才找到
/**//*
文件名:jquery.liu.select.js
功能說明:本js文件為jquery類庫的一個插件,主要實現(xiàn)對select的操作.
作者:John Liu
編寫日期:2008/03/12
*/
//得到select項的個數(shù)
jQuery.fn.size = function()
{
return jQuery(this).get(0).options.length;
}
//獲得選中項的索引
jQuery.fn.getSelectedIndex = function()
{
return jQuery(this).get(0).selectedIndex;
}
//獲得當(dāng)前選中項的文本
jQuery.fn.getSelectedText = function()
{
if(this.size() == 0)
{
return "下拉框中無選項";
}
else
{
var index = this.getSelectedIndex();
return jQuery(this).get(0).options[index].text;
}
}
//獲得當(dāng)前選中項的值
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中選定的項
jQuery.fn.removeSelected = function()
{
var index = this.getSelectedIndex();
this.removeIndex(index);
}
//清除select中的所有項
jQuery.fn.clearAll = function()
{
jQuery(this).get(0).options.length = 0;
}
復(fù)制代碼 代碼如下:
/**//*
文件名:jquery.liu.select.js
功能說明:本js文件為jquery類庫的一個插件,主要實現(xiàn)對select的操作.
作者:John Liu
編寫日期:2008/03/12
*/
//得到select項的個數(shù)
jQuery.fn.size = function()
{
return jQuery(this).get(0).options.length;
}
//獲得選中項的索引
jQuery.fn.getSelectedIndex = function()
{
return jQuery(this).get(0).selectedIndex;
}
//獲得當(dāng)前選中項的文本
jQuery.fn.getSelectedText = function()
{
if(this.size() == 0)
{
return "下拉框中無選項";
}
else
{
var index = this.getSelectedIndex();
return jQuery(this).get(0).options[index].text;
}
}
//獲得當(dāng)前選中項的值
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中選定的項
jQuery.fn.removeSelected = function()
{
var index = this.getSelectedIndex();
this.removeIndex(index);
}
//清除select中的所有項
jQuery.fn.clearAll = function()
{
jQuery(this).get(0).options.length = 0;
}
您可能感興趣的文章:
- 基于JQuery的日期聯(lián)動實現(xiàn)代碼
- jquery+json 通用三級聯(lián)動下拉列表
- 簡單實用jquery版三級聯(lián)動select示例
- jquery 實現(xiàn)二級/三級/多級聯(lián)動菜單的思路及代碼
- JQuery打造省市下拉框聯(lián)動效果
- jquery ajax實現(xiàn)下拉框三級無刷新聯(lián)動,且保存保持選中值狀態(tài)
- jQuery 聯(lián)動日歷實現(xiàn)代碼
- 基于JQUERY的多級聯(lián)動代碼
- Jquery實現(xiàn)無刷新DropDownList聯(lián)動實現(xiàn)代碼
- jQuery制作簡潔的多級聯(lián)動Select下拉框
- 用Jquery實現(xiàn)多級下拉框無刷新的聯(lián)動
- jQuery 下拉列表 二級聯(lián)動插件分享
- jQuery實現(xiàn)日期聯(lián)動效果實例
相關(guān)文章
jQuery插件FusionCharts繪制2D柱狀圖和折線圖的組合圖效果示例【附demo源碼】
這篇文章主要介紹了jQuery插件FusionCharts繪制2D柱狀圖和折線圖的組合圖效果,結(jié)合完整實例形式分析了jQuery使用插件FusionCharts載入xml格式數(shù)據(jù)繪制柱狀圖與折線圖組合圖效果的操作步驟與相關(guān)實現(xiàn)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-04-04jQuery中closest和parents的區(qū)別分析
本文給大家介紹jquery中parents()和closest()用法與區(qū)別介紹,在jquery中parents()查找父級元素刪除的時候,發(fā)現(xiàn)它不包含根元素,于是用了closest(),效果不錯,下面我來給大家具體的介紹一下2015-05-05解決jQuery插件tipswindown與hintbox沖突
先掃下盲:tipswindown是jQuery的彈窗插件,可以使用url或當(dāng)前頁元素顯示在模擬層中;hintbox是jQuery的類似Google Suggestions插件。2010-11-11jquery獲取并修改觸發(fā)事件的DOM元素示例【基于target 屬性】
這篇文章主要介紹了jquery獲取并修改觸發(fā)事件的DOM元素,結(jié)合實例形式分析了jQuery基于target 屬性獲取到觸發(fā)該事件的dom并修改的相關(guān)操作技巧,需要的朋友可以參考下2019-10-10jQuery源碼分析-04 選擇器-Sizzle-工作原理分析
在分析Sizzle源碼之前,先整理一下選擇器的工作原理,先明確一些選擇器中用到的名詞,后邊閱讀時不會有歧義2011-11-11jquery ajax雙擊div可直接修改div中的內(nèi)容
這篇文章主要介紹了jquery ajax雙擊div直接修改div中內(nèi)容的相關(guān)方法,感興趣的小伙伴們可以參考一下2016-03-03