js multiple全选与取消全选实现代码

时间:2024-04-28 10:19:36 


$("select").change(function(){
    var n = $(this).children().length;
    var obj;
    var i = 1;
       $(this).children().each(function(){
     if(i == n)
     {
      alert($(this).text());
     }
     i++;
    });
   });


综合以上二法,本人轻易得出了自己想要的效果,如下:


function selectall()
{
 $("select").children().each(function(){$(this).attr("selected","selected")})
}


分别用到了 children()和$(this).attr("selected","selected")
一个js用法


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript">
<!--
function selectAll()
{
 var opts=document.getElementById("CourseList");
 for(var i=0;i<opts.length;i++)
 {
  opts[i].selected=true;
 }
}
//-->
</script>
</head>
<body>
<select id="CourseList" name="CourseList" multiple="multiple" size="20" style="width:230px;">
<option value="003" >(003) 消费者行为与销售心理</option>
<option value="004" >(004) 产品策划与市场推广</option>
<option value="005" >(005) 品牌定位与广告原理</option>
</select>
<input type="button" onclick="selectAll()" value="全选">
</form>
</body>
</html>
标签:全选,取消全选
0
投稿

猜你喜欢

  • CSS网页布局扩展小技巧

    2010-06-03 12:13:00
  • 总结MySQL修改最大连接数的两个方式

    2024-01-27 22:55:55
  • Python turtle库绘制菱形的3种方式小结

    2022-04-10 14:08:19
  • Win10下为VSCode配置LaTex编辑器的方法

    2023-08-27 17:20:07
  • ASP 操作cookies的方法

    2011-03-10 11:24:00
  • Golang Compare And Swap算法详细介绍

    2024-02-19 16:08:02
  • Python源码学习之PyObject和PyTypeObject

    2023-08-11 10:28:45
  • Python常用模块介绍

    2021-01-03 10:11:53
  • 使用 XSLT 解释 XML 文件

    2009-03-08 19:06:00
  • 浅析is_writable的php实现

    2023-09-09 01:41:05
  • ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能示例

    2023-11-17 02:54:39
  • 基于Python实现中秋佳节月饼抢购脚本

    2023-02-23 12:48:39
  • K最近邻算法(KNN)---sklearn+python实现方式

    2023-09-14 15:29:31
  • Golang单元测试与覆盖率的实例讲解

    2024-05-05 09:27:50
  • node.js支持多用户web终端实现及安全方案

    2024-05-05 09:22:10
  • python日志模块loguru详解

    2023-10-23 20:50:08
  • MySQL SQL预处理(Prepared)的语法实例与注意事项

    2024-01-14 17:50:09
  • vue实现拖拽交换位置

    2024-04-30 08:45:51
  • 利用ImageAI库只需几行python代码实现目标检测

    2023-06-25 15:03:28
  • python批量添加zabbix Screens的两个脚本分享

    2022-07-21 02:17:28
  • asp之家 网络编程 m.aspxhome.com