A-A+
js multiple全选与取消全选实现代码
本文章总结了利用jquery与js实现multiple全选与取消全选代码有需要参考的朋友可参考参考,代码如下:
- $("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>