JQuery 操作select標簽實現(xiàn)代碼
更新時間:2010年05月14日 11:17:03 作者:
我們經(jīng)常遇到要操作DOM元素,例如<select>,在Asp.net中Dropdownlist原型就是select。
下面幾個常用的代碼或許對您有幫助:
//1.獲取選中option值
$('#selectList').val();
//2.獲取選中option的文本
$('#selectList :selected').text();
//3.獲取多個選中option值、文本
var foo = [];
$('#multiple :selected').each(function(i, selected) {
foo[i] = $(selected).text();
});
// to get the selected values, just use .val() - this returns a string or array
foo = $('#multiple :selected').val();
//4.使用選項option的條件表達式
switch ($('#selectList :selected').text()) {
case 'First Option':
//do something
break;
case 'Something Else':
// do something else
break;
}
//5.刪除某個value=2的option
$("#selectList option[value='2']").remove();
//6.從list A 移動option到 list B.
// here we have 2 select lists and 2 buttons. If you click the “add” button,
// we remove the selected option from select1 and add that same option to select2.
// The “remove” button just does things the opposite way around.
// Thanks to jQuery's chaining capabilities, what was once a rather tricky undertaking with JS can now be done in 6 lines of code.
$().ready(function() {
$('#add').click(function() {
return !$('#select1 option:selected').appendTo('#select2');
});
$('#remove').click(function() {
return !$('#select2 option:selected').appendTo('#select1');
});
});
復(fù)制代碼 代碼如下:
//1.獲取選中option值
$('#selectList').val();
//2.獲取選中option的文本
$('#selectList :selected').text();
//3.獲取多個選中option值、文本
var foo = [];
$('#multiple :selected').each(function(i, selected) {
foo[i] = $(selected).text();
});
// to get the selected values, just use .val() - this returns a string or array
foo = $('#multiple :selected').val();
//4.使用選項option的條件表達式
switch ($('#selectList :selected').text()) {
case 'First Option':
//do something
break;
case 'Something Else':
// do something else
break;
}
//5.刪除某個value=2的option
$("#selectList option[value='2']").remove();
//6.從list A 移動option到 list B.
// here we have 2 select lists and 2 buttons. If you click the “add” button,
// we remove the selected option from select1 and add that same option to select2.
// The “remove” button just does things the opposite way around.
// Thanks to jQuery's chaining capabilities, what was once a rather tricky undertaking with JS can now be done in 6 lines of code.
$().ready(function() {
$('#add').click(function() {
return !$('#select1 option:selected').appendTo('#select2');
});
$('#remove').click(function() {
return !$('#select2 option:selected').appendTo('#select1');
});
});
如果您不了解JQuery,可以先看它的文檔。
希望這篇POST對您有幫助。
相關(guān)文章
淺析JQuery獲取和設(shè)置Select選項的常用方法總結(jié)
本篇文章是對JQuery獲取和設(shè)置Select選項的一些常用方法進行了匯總介紹,有需要的朋友可以參考一下2013-07-07
jquery控制頁面的展開和隱藏實現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄猨query控制頁面的展開和隱藏實現(xiàn)方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
jQuery實現(xiàn)帶右側(cè)索引功能的通訊錄示例【附源碼下載】
這篇文章主要介紹了jQuery實現(xiàn)帶右側(cè)索引功能的通訊錄,結(jié)合實例形式分析了jQuery針對頁面元素動態(tài)遍歷、排序等相關(guān)操作技巧,并附源碼供讀者下載參考,需要的朋友可以參考下2018-04-04
jQuery使用jsonp實現(xiàn)百度搜索的示例代碼
這篇文章主要介紹了jQuery使用jsonp實現(xiàn)百度搜索,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下2020-07-07

