1.獲取列表項中候選項的數目。
2.獲得選中項的索引值。
3.獲得當前選中項的值
4.設定選擇值
5.設定選擇項
...
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;
}
---------------------------------------------------------
以下來自夢三秋:http://www.skygq.com/2010/12/24/jquery-process-select-demo/
<script type="text/javascript">
$(document).ready(function(){
//添加“江蘇”到下拉框的最後一位
$('#add_to_last').click(function(){
$('#select').append('<OPTION value="江蘇">江蘇</OPTION>');
});
//添加“安徽”到下拉框的第一位
$('#add_to_first').click(function(){
$('#select').prepend('<OPTION value="安徽">安徽</OPTION>');
});
//獲取當前的selectIndex(當前選中的下拉菜單的項目的索引)
$('#get_select_index').click(function(){
alert($('#select option:selected').attr("index"));
});
//移除下拉菜單最後一個項目
$('#remove_last_option').click(function(){
$('#select option:last').remove();
});
//移除除了第一個之外的所有選項
$('#remove_all_option_except_first').click(function(){
$('#select option').not(':first').remove();
});
//獲取下拉菜單最大索引值
$('#get_max_index').click(function(){
var maxIndex=$("#select option:last").attr("index");
alert(maxIndex);
});
});
</script>
<SELECT id="select" style="width:100px"> <OPTION selected>中國省份</OPTION></SELECT> <p/>
<button id="add_to_last">添加江蘇到最後一位</button><br/>
<button id="add_to_first">添加安徽到第一位</button><br/>
<button id="get_select_index">獲取當前的selectIndex</button><br/>
<button id="remove_last_option">移除下拉菜單最後一個項目</button><br/>
<button id="remove_all_option_except_first">移除除了第一個之外的所有選項</button><br/>
<button id="get_max_index">獲取下拉菜單最大索引值</button><br/>