javascript对下拉列表框(select)的操作

[html] view plaincopy
 
  1. <form id="f">   
  2. <select size="1" name="s">   
  3. <option value="www.dwww.cn">设计家园</option>   
  4. <option value="baidu.com">百度</option>   
  5. </select>   
  6. </form>  
  7.   
  8. ---------------------------------------------------------------------------  
  9.   
  10. <script type="text/javascript">   
  11. <!--   
  12. var f = document.getElementById("f");  
  13.   
  14. //获得select列表项数目   
  15. document.write(f.s.options.length);   
  16. document.write(f.s.length);  
  17.   
  18. //当前选中项的下标(从0 开始)(有两种方法)   
  19. //如果选择了多项,则返回第一个选中项的下标   
  20. document.write(f.s.options.selectedIndex);   
  21. document.write(f.s.selectedIndex);  
  22.   
  23. //检测某一项是否被选中   
  24. document.write(f.s.options[0].selected);  
  25.   
  26. //获得某一项的值和文字   
  27. document.write(f.s.options[0].value);   
  28. document.write(f.s.options[1].text);  
  29.   
  30. //删除某一项   
  31. f.s.options[1] = null;  
  32.   
  33. //追加一项   
  34. f.s.options[f.s.options.length] = new Option("追加的text", "追加的value");  
  35.   
  36. //更改一项   
  37. f.s.options[1] = new Option("更改的text", "更改的value");   
  38. //也可以直接设置该项的 text 和 value   
  39. //-->   
  40. </script>  
  41.   
  42.   
  43.   
  44.   
  45. //全选列表中的项   
  46. function SelectAllOption(list)   
  47. {   
  48. for (var i=0; i<list.options.length; i++)   
  49. {   
  50. list.options[i].selected = true;   
  51. }   
  52. }  
  53.   
  54.   
  55. //反选列表中的项   
  56. function DeSelectOptions(list)   
  57. {   
  58. for (var i=0; i<list.options.length; i++)   
  59. {   
  60. list.options[i].selected = !list.options[i].selected;   
  61. }   
  62. }  
  63.   
  64.   
  65. //返回列表中选择项数目   
  66. function GetSelectedOptionsCnt(list)   
  67. {   
  68. var cnt = 0;   
  69. var i = 0;   
  70. for (i=0; i<list.options.length; i++)   
  71. {   
  72. if (list.options[i].selected)   
  73. {   
  74. cnt++;   
  75. }   
  76. }  
  77.   
  78. return cnt;   
  79. }  
  80.   
  81.   
  82. //清空列表   
  83. function ClearList(list)   
  84. {   
  85. while (list.options.length > 0)   
  86. {   
  87. list.options[0] = null;   
  88. }   
  89. }  
  90.   
  91.   
  92. //删除列表选中项   
  93. //返回删除项的数量   
  94. function DelSelectedOptions(list)   
  95. {   
  96. var i = 0;   
  97. var deletedCnt = 0;   
  98. while (i list.options.length)   
  99. {   
  100. if (list.options[i].selected)   
  101. {   
  102. list.options[i] = null;   
  103. deletedCnt++;   
  104. }   
  105. else   
  106. {   
  107. i++;   
  108. }   
  109. }  
  110.   
  111. return deletedCnt;   
  112. }   
  113. //此函数查找相应的项是否存在   
  114. //repeatCheck是否进行重复性检查   
  115. //若为"v",按值进行重复值检查   
  116. //若为"t",按文字进行重复值检查   
  117. //若为"vt",按值和文字进行重复值检查   
  118. //其它值,不进行重复性检查,返回false   
  119. function OptionExists(list, optText, optValue, repeatCheck)   
  120. {   
  121. var i = 0;   
  122. var find = false;  
  123.   
  124. if (repeatCheck == "v")   
  125. {   
  126. //按值进行重复值检查   
  127. for (i=0; i<list.options.length; i++)   
  128. {   
  129. if (list.options[i].value == optValue)   
  130. {   
  131. find = true;   
  132. break;   
  133. }   
  134. }   
  135. }   
  136. else if (repeatCheck == "t")   
  137. {   
  138. //按文字进行重复检查   
  139. for (i=0; i<list.options.length; i++)   
  140. {   
  141. if (list.options[i].text == optText)   
  142. {   
  143. find = true;   
  144. break;   
  145. }   
  146. }   
  147. }   
  148. else if (repeatCheck == "vt")   
  149. {   
  150. //按值和文字进行重复检查   
  151. for (i=0; i<list.options.length; i++)   
  152. {   
  153. if ((list.options[i].value == optValue) && (list.options[i].text == optText))   
  154. {   
  155. find = true;   
  156. break;   
  157. }   
  158. }   
  159. }  
  160.   
  161. return find;   
  162. }  
  163.   
  164.   
  165. //向列表中追加一个项   
  166. //list 是要追加的列表   
  167. //optText 和 optValue 分别表示项的文字和值   
  168. //repeatCheck 是否进行重复性检查,参见 OptionExists   
  169. //添加成功返回 true,失败返回 false   
  170. function AppendOption(list, optText, optValue, repeatCheck)   
  171. {   
  172. if (!OptionExists(list, optText, optValue, repeatCheck))   
  173. {   
  174. list.options[list.options.length] = new Option(optText, optValue);   
  175. return true;   
  176. }   
  177. else   
  178. {   
  179. return false;   
  180. }   
  181. }  
  182.   
  183.   
  184. //插入项   
  185. //index 插入位置,当插入位置 >= 列表现有项数量时,其作用相当于不进行重复检查的追加项   
  186. //optText 和 optValue 分别表示项的文字和值   
  187. function InsertOption(list, index, optText, optValue)   
  188. {   
  189. var i = 0;   
  190. for (i=list.options.length; i>index; i--)   
  191. {   
  192. list.options[i] = new Option(list.options[i-1].text, list.options[i-1].value);   
  193. }  
  194.   
  195. list.options[index] = new Option(optText, optValue);   
  196. }   
  197. //将一个列表中的项导到另一个列表中   
  198. //repeatCheck是否进行重复性检查,参见OptionExists   
  199. //deleteSource项导到目标后,是否删除源列表中的项   
  200. //返回影响的项数量   
  201. function ListToList(sList, dList, repeatCheck, deleteSource)   
  202. {   
  203. //所影响的行数   
  204. var lines = 0;   
  205. var i = 0;   
  206. while (i<sList.options.length)   
  207. {   
  208. if (sList.options[i].selected && AppendOption(dList, sList.options[i].text, sList.options[i].value, repeatCheck))   
  209. {   
  210. //添加成功   
  211. lines++;   
  212. if (deleteSource)   
  213. {   
  214. //删除源列表中的项   
  215. sList.options[i] = null;   
  216. }   
  217. else   
  218. {   
  219. i++;   
  220. }   
  221. }   
  222. else   
  223. {   
  224. i++;   
  225. }   
  226. }  
  227.   
  228. return lines;   
  229. }  
  230.   
  231.   
  232. //列表中选中项上移   
  233. function MoveSelectedOptionsUp(list)   
  234. {   
  235. var i = 0;   
  236. var value = "";   
  237. var text = "";   
  238. for (i=0; i<(list.options.length-1); i++)   
  239. {   
  240. if (!list.options[i].selected && list.options[i+1].selected)   
  241. {   
  242. value = list.options[i].value;   
  243. text = list.options[i].text;   
  244. list.options[i] = new Option(list.options[i+1].text, list.options[i+1].value);   
  245. list.options[i].selected = true;   
  246. list.options[i+1] = new Option(text, value);   
  247. }   
  248. }   
  249. }  
  250.   
  251.   
  252. //列表中选中项下移   
  253. function MoveSelectedOptionsDown(list)   
  254. {   
  255. var i = 0;   
  256. var value = "";   
  257. var text = "";   
  258. for (i=list.options.length-1; i>0; i--)   
  259. {  
  260.   
  261.   
  262. if (!list.options[i].selected && list.options[i-1].selected)   
  263. {   
  264. value = list.options[i].value;   
  265. text = list.options[i].text;   
  266. list.options[i] = new Option(list.options[i-1].text, list.options[i-1].value);   
  267. list.options[i].selected = true;   
  268. list.options[i-1] = new Option(text, value);   
  269. }   
  270. }   
  271. }
原文地址:https://www.cnblogs.com/yanghj010/p/5109699.html