JQuery select标签操作代码段

2019-12-24,,,

下面几个常用的代码或许对您有帮助:
复制代码 代码如下:
//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,可以先看它的文档。

您可能感兴趣的文章:

  • 50个比较实用jQuery代码段
  • 分享javascript、jquery实用代码段
  • 利用Jquery实现几款漂亮实用的时间轴(附示例代码)
  • jQuery+ajax实现实用的点赞插件代码
  • 分享12个实用的jQuery代码片段
  • 基于jQuery实现美观且实用的倒计时实例代码
  • 8个超实用的jQuery功能代码分享
  • 60个很实用的jQuery代码开发技巧收集
  • 一些实用的jQuery代码片段收集
  • jquery实用代码片段集合
  • 非常实用的jQuery代码段集锦【检测浏览器、滚动、复制、淡入淡出等】

《JQuery select标签操作代码段.doc》

下载本文的Word格式文档,以方便收藏与打印。