熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

利用jquery 控制select 實例代碼

2022-06-13   來源: Java核心技術 

//獲取選中option值
$(#selectList)val();
//獲取選中option的文本
$(#selectList :selected)text();
//獲取多個選中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();
//使用選項option的條件表達式
switch ($(#selectList :selected)text()) {
case First Option:
//do something
break;
case Something Else:
// do something else
break;
}
//刪除某個value=的option
$("#selectList option[value=]")remove();
//從list A 移動option到 list B
// here we have select lists and buttons If you click the “add” button
// we remove the selected option from select and add that same option to select
// The “remove” button just does things the opposite way around
// Thanks to jQuerys chaining capabilities what was once a rather tricky undertaking with JS can now be done in lines of code
$()ready(function() {
$(#add)click(function() {
return !$(#select option:selected)appendTo(#select);
});
$(#remove)click(function() {
return !$(#select option:selected)appendTo(#select);
});
});



From:http://tw.wingwit.com/Article/program/Java/hx/201405/30809.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.