posts - 930,  comments - 588,  views - 402万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

        我们经常遇到要操作DOM元素,例如<select>,在Asp.net中Dropdownlist原型就是select。下面几个常用的代码或许对您有帮助:

   1:      //1.获取选中option值
   2:      $('#selectList').val();
   3:   
   4:      //2.获取选中option的文本
   5:      $('#selectList :selected').text();
   6:   
   7:   
   8:      //3.获取多个选中option值、文本
   9:      var foo = [];
  10:      $('#multiple :selected').each(function(i, selected) {
  11:          foo[i] = $(selected).text();
  12:      });
  13:      // to get the selected values, just use .val() - this returns a string or array
  14:      foo = $('#multiple :selected').val();
  15:   
  16:   
  17:      //4.使用选项option的条件表达式
  18:      switch ($('#selectList :selected').text()) {
  19:          case 'First Option':
  20:              //do something
  21:              break;
  22:          case 'Something Else':
  23:              // do something else
  24:   
  25:              break;
  26:      }
  27:   
  28:      //5.删除某个value=2的option 
  29:      $("#selectList option[value='2']").remove();
  30:      
  31:   
  32:      //6.从list A 移动option到 list B.
  33:       // here we have 2 select lists and 2 buttons. If you click the “add” button, 
  34:      // we remove the selected option from select1 and add that same option to select2.
  35:      // The “remove” button just does things the opposite way around. 
  36:      // Thanks to jQuery’s chaining capabilities, what was once a rather tricky undertaking with JS can now be done in 6 lines of code.
  37:      $().ready(function() {
  38:          $('#add').click(function() {
  39:              return !$('#select1 option:selected').appendTo('#select2');
  40:          });
  41:          $('#remove').click(function() {
  42:              return !$('#select2 option:selected').appendTo('#select1');
  43:          });
  44:      });

如果您不了解JQuery,可以先看它的文档

希望这篇POST对您有帮助。

Author: Petter Liu  http://www.cnblogs.com/wintersun

posted on   PetterLiu  阅读(1089)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
点击右上角即可分享
微信分享提示