javascript

位置:IT落伍者 >> javascript >> 浏览文章

利用jquery 控制select 实例代码


发布日期:2023年01月14日
 
利用jquery 控制select 实例代码

//获取选中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);
});
});

               

上一篇:jquery动态获取图片高度与宽度

下一篇:jquery 常用使用方法