select下拉框学习总结

关于下拉框的一些简单使用,希望对您的学习有所帮助 

 
 

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  2. <html xmlns="http://www.w3.org/1999/xhtml"
  3. <head> 
  4.     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
  5.     <title>select学习-之传统JS</title> 
  6.     <script type="text/javascript" src="jquery1.5.js"></script> 
  7.     <script type="text/javascript"
  8.         /**根据游戏显示游戏对应的相应的区!!*/ 
  9.         function gameselect() { 
  10.             //索引,减一是为了忽略第一个“请选择游戏” 
  11.             //var index = document.getElementById("gameselect").selectedIndex - 1; 
  12.             /*****------把上面一句代码换成Jquery为:-------****/ 
  13.             var index = $("#gameselect").get(0).selectedIndex - 1; 
  14.              
  15.             //清除所选过的 
  16.         //  document.getElementById("gameareaselect").options.length = 1; 
  17.             /**------把上面一句代码换成Jquery为:---*/ 
  18.             $("#gameareaselect").get(0).options.length = 1; 
  19.              
  20.              
  21.             //声明一个数组做测试数据 
  22.             var gameareaArray = new Array(); 
  23.             gameareaArray[0] = ["坦克区1","坦克区2"]; 
  24.             gameareaArray[1] = ["激战区1","激战区2","激战区3","激战区4"]; 
  25.             gameareaArray[2] = ["功夫英雄区1","功夫英雄区2","功夫英雄区3"]; 
  26.              
  27.             for(var i in gameareaArray[index]) { 
  28.                 var option = new Option(gameareaArray[index][i],gameareaArray[index][i]); 
  29.                 document.getElementById("gameareaselect").options.add(option); 
  30.             } 
  31.         } 
  32.  
  33.         function gameareaSelect() { 
  34.             var value = document.getElementById("gameareaselect").value; 
  35.             alert("下拉框的value值为:" + value ); 
  36.              
  37.             //下面我们去拿显示的值 
  38.         //  var gamearea = document.getElementById("gameareaselect"); 
  39.         //  var showValue = gamearea.options[gamearea.selectedIndex].text; 
  40.             /**---把上面两行代码换成下面Jquery形式--*/ 
  41.             var  index = $("#gameareaselect").get(0).selectedIndex ; 
  42.             var showValue = $("#gameareaselect").get(0).options[index].text; 
  43.              
  44.             alert("显示的值为:" + showValue); 
  45.         } 
  46.     </script> 
  47. </head> 
  48. <body> 
  49.     <select name="" id="gameselect" οnchange="gameselect()"
  50.         <option value="00">--请选择游戏--</option> 
  51.         <option value="10">坦克世界</option> 
  52.         <option value="20">激战2</option> 
  53.         <option value="30">功夫英雄</option> 
  54.     </select> 
  55.     <select name="" id="gameareaselect" οnchange="gameareaSelect()"
  56.         <option value="00">--请选择区--</option> 
  57.     </select> 
  58. </body> 
  59. </html> 

2.

 
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4.     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> 
  5.     <title>select学习-之传统JS</title> 
  6.     <script type="text/javascript" src="jquery1.5.js"></script> 
  7.     <script type="text/javascript"> 
  8.         /**根据游戏显示游戏对应的相应的区!!*/ 
  9.         function gameselect() { 
  10.             //var index = document.getElementById("gameselect").value; 
  11.             /******-把上面的代码改成Jquery--******/ 
  12.             var index = $("#gameselect").get(0).value; 
  13.              
  14.             //清除所选过的 
  15.         //  document.getElementById("gameareaselect").options.length = 1
  16.             /*****------把上面的代码换成Jquery代码------*****/ 
  17.             $("#gameareaselect").get(0).options.length = 1
  18.              
  19.             //声明一个数组做测试数据 
  20.             var gameareaArray = new Array(); 
  21. //          gameareaArray[0] = ["坦克区1","坦克区2"]; 
  22. //          gameareaArray[1] = ["激战区1","激战区2","激战区3","激战区4"]; 
  23. //          gameareaArray[2] = ["功夫英雄区1","功夫英雄区2","功夫英雄区3"]; 
  24.             //数组的下标可以为字符串,比如换成下面的 
  25.              
  26.             gameareaArray['tanke'] = ["坦克区1","坦克区2"]; 
  27.             gameareaArray['jizhan'] = ["激战区1","激战区2","激战区3","激战区4"]; 
  28.             gameareaArray['gongfu'] = ["功夫英雄区1","功夫英雄区2","功夫英雄区3"]; 
  29.              
  30.             for(var i in gameareaArray[index]) { 
  31.                 var option = new Option(gameareaArray[index][i],gameareaArray[index][i]); 
  32.             //  var option = new Option(gameareaArray[index][i],"00000"); 
  33.                 document.getElementById("gameareaselect").options.add(option); 
  34.             } 
  35.         } 
  36.  
  37.         function gameareaSelect() { 
  38.             var value = document.getElementById("gameareaselect").value; 
  39.             alert("下拉框的value值为:" + value ); 
  40.              
  41.             //下面我们去拿显示的值 
  42.         //  var gamearea = document.getElementById("gameareaselect"); 
  43.         //  var showValue = gamearea.options[gamearea.selectedIndex].text; 
  44.                 /**---把上面两行代码换成下面Jquery形式--*/ 
  45.             var  index = $("#gameareaselect").get(0).selectedIndex ; 
  46.             var showValue = $("#gameareaselect").get(0).options[index].text; 
  47.              
  48.             alert("显示的值为:" + showValue); 
  49.         } 
  50.          
  51.         /**删除游戏“功夫英雄”*/ 
  52.         function delGongfu() { 
  53.             $("#gameselect").get(0).remove(3); 
  54.         } 
  55.     </script> 
  56. </head> 
  57. <body> 
  58.     <select name="" id="gameselect" onchange="gameselect()"> 
  59.         <option value="00">--请选择游戏--</option> 
  60.         <option value="tanke">坦克世界</option> 
  61.         <option value="jizhan">激战2</option> 
  62.         <option id="gongfu" value="gongfu">功夫英雄</option> 
  63.     </select> 
  64.     <select name="" id="gameareaselect" onchange="gameareaSelect()"> 
  65.         <option value="00">--请选择区--</option> 
  66.     </select> 
  67.      
  68.     <input type="button" value="删除功夫英雄" onclick="delGongfu()" /> 
  69. </body> 
  70. </html> 

3.

 

 
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4.     <meta http-equiv="Content-Type" content="text/html;charset=GBK" /> 
  5.     <title>jquery之select学习---有自己封装的对select的操作</title> 
  6.     <script type="text/javascript" src="jquery1.5.js"></script> 
  7.     <script type="text/javascript" src="jquery_select.js"></script> 
  8.     <script type="text/javascript"> 
  9. /*      $(document).ready(function(){ 
  10.             alert(1); 
  11.         }); 
  12.          
  13.          
  14.         jQuery(document).ready(function(){ 
  15.             alert(2); 
  16.         }); 
  17. */ 
  18.  
  19.         //自己创建jQuery简写 
  20.         var $j = jQuery.noConflict(); 
  21.         $j(document).ready(function(){ 
  22.              
  23.             //1.添加options 
  24.             $j("#bt").click(function(){ 
  25.                 $j("#demo").addOption("请选择","00"); 
  26.                 $j("#demo").addOption("China","china"); 
  27.                 $j("#demo").addOption("USA","usa"); 
  28.                  
  29.                 //换成Javascript代码如下 
  30.                 document.getElementById("demo").options.add(new Option("Franch","france")); 
  31.             }); 
  32.              
  33.             //2.改变options的个数 
  34.             $j("#bt1").click(function(){ 
  35.                 //$j("#demo").clearAll(); 
  36.                 //上面一行代码换成JavaScript 
  37.                 //document.getElementById("demo").options.length = 1
  38.                  
  39.                 //$j("#demo").options.length = 0; 记得这么写是不对的。。。。。应该是下面的写法 
  40.                 //$j("#demo").get(0).options.length = 1;     
  41.                  
  42.                 $j("#demo").get(0).remove(3);//删除索引为3的option 
  43.             }); 
  44.              
  45.             //3.确定options的个数 
  46.             $j("#bt2").click(function(){ 
  47.                 //  var size = $j("#demo").optionssize(); 
  48.                 //换成javascript为: 
  49.                 var size = document.getElementById("demo").options.length; 
  50.                 alert(size); 
  51.             }); 
  52.              
  53.         }); 
  54.  
  55.         //4.确定当前是第几个options 
  56.         function getCount() { 
  57.             //  var index = document.getElementById("demo").selectedIndex; 
  58.             //换成对应的jQuery代码为: 
  59.             var index = $j("#demo").get(0).selectedIndex; 
  60.              
  61.             var text = $j("#demo").get(0).options[index].text; 
  62.          
  63.             alert(index + ":" + text); 
  64.         } 
  65.  
  66. /* 
  67.         jQuery(document).ready(function($){ 
  68.             $("#bt").click(function(){ 
  69.                 alert(1); 
  70.             }); 
  71.         }); 
  72. */ 
  73.     </script> 
  74. </head> 
  75. <body> 
  76.     <input type="button" value="触发" id="bt"/> 
  77.     <input type="button" value="消失" id="bt1"/> 
  78.     <input type="button" value="options的个数" id="bt2"/> 
  79.      
  80.     <select name="demo" id="demo" onchange="getCount()"> 
  81.          
  82.     </select> 
  83. </body> 
  84. </html> 

4.

 
 
  1. //get select size 
  2. jQuery.fn.optionssize = function(){ 
  3.     return jQuery(this).get(0).options.length; 
  4. //get selected index 
  5. jQuery.fn.getSelectedIndex = function(){ 
  6.     return jQuery(this).get(0).selectedIndex; 
  7. jQuery.fn.getSelectedOption = function(){ 
  8.     var index = this.getSelectedIndex(); 
  9.     return jQuery(this).get(0).options[index]; 
  10. //get selected text 
  11. jQuery.fn.getSelectedText = function(){ 
  12.     if(this.optionssize() == 0)  return ""
  13.     else
  14.         var index = this.getSelectedIndex(); 
  15.         return jQuery(this).get(0).options[index].text; 
  16.     } 
  17. //get selected attr 
  18. jQuery.fn.getSelectedAttr = function(attrName){ 
  19.     if(this.optionssize() == 0)  return ""
  20.     else
  21.         var index = this.getSelectedIndex(); 
  22.         return jQuery(jQuery(this).get(0).options[index]).attr(attrName); 
  23.     } 
  24. //get selected value 
  25. jQuery.fn.getSelectedValue = function(){ 
  26.     if(this.optionssize() == 0) 
  27.         return ""
  28.     else   
  29.         return jQuery(this).val(); 
  30. }  
  31. //set option value select 
  32. jQuery.fn.setSelectedValue = function(value){   
  33.     jQuery(this).get(0).value = value;   
  34. }   
  35.    
  36. //set option text first select 
  37. jQuery.fn.setSelectedText = function(text)   
  38. {   
  39.     var isExist = false;   
  40.     var count = this.optionssize();   
  41.     for(var i=0;i<count;i++)   
  42.     {   
  43.         if(jQuery(this).get(0).options[i].text == text)   
  44.         {   
  45.             jQuery(this).get(0).options[i].selected = true;   
  46.             isExist = true;   
  47.             break;   
  48.         }   
  49.     }   
  50. }   
  51. //set selected index 
  52. jQuery.fn.setSelectedIndex = function(index)   
  53. {   
  54.     var count = this.optionssize();       
  55.     if(index >= count || index < 0)   
  56.     {   
  57.         jQuery(this).get(0).selectedIndex = -1; 
  58.     }   
  59.     else   
  60.     {   
  61.         jQuery(this).get(0).selectedIndex = index;   
  62.     }   
  63. }   
  64. //check select has value 
  65. jQuery.fn.isExistItem = function(value)   
  66. {   
  67.     var isExist = false;   
  68.     var count = this.optionssize();   
  69.     for(var i=0;i<count;i++)   
  70.     {   
  71.         if(jQuery(this).get(0).options[i].value == value)   
  72.         {   
  73.             isExist = true;   
  74.             break;   
  75.         }   
  76.     }   
  77.     return isExist;   
  78. }   
  79. //add select value,text if(this has alert) 
  80. jQuery.fn.addOption = function(text,value)   
  81. {   
  82.     if(this.isExistItem(value))   
  83.     {   
  84.          
  85.     }   
  86.     else   
  87.     {   
  88.         jQuery(this).get(0).options.add(new Option(text,value));   
  89.     }   
  90. }   
  91. //delete select by value if(this has not alert) 
  92. jQuery.fn.removeItem = function(value)   
  93. {       
  94.     if(this.isExistItem(value))   
  95.     {   
  96.         var count = this.optionssize();           
  97.         for(var i=0;i<count;i++)   
  98.         {   
  99.             if(jQuery(this).get(0).options[i].value == value)   
  100.             {   
  101.                 jQuery(this).get(0).remove(i);   
  102.                 break;   
  103.             }   
  104.         }           
  105.     }   
  106.     else   
  107.     {   
  108.           
  109.     }   
  110. }   
  111. //delete select by index 
  112. jQuery.fn.removeIndex = function(index)   
  113. {   
  114.     var count = this.optionssize();   
  115.     if(index >= count || index < 0)   
  116.     {   
  117.          
  118.     }   
  119.     else   
  120.     {   
  121.         jQuery(this).get(0).remove(index);   
  122.     }   
  123. }   
  124. //delete select by seleced 
  125. jQuery.fn.removeSelected = function()   
  126. {   
  127.     var index = this.getSelectedIndex();   
  128.     this.removeIndex(index);   
  129. }   
  130. //clear all options 
  131. jQuery.fn.clearAll = function()   
  132. {   
  133.     jQuery(this).get(0).options.length = 0;   
  134. }   

具体例子请去下载:select总结


     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/1151983 ,如需转载请自行联系原作者




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值