Posted By

mn232nm on 11/26/08


Tagged

javascript select


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

guido


ListUtil


 / Published in: JavaScript
 

对select元素(dropdownlist)的操作,包括添加删除等操作

  1. var ListUtil = new Object();
  2.  
  3. ListUtil.getSelectedIndexes = function (oListbox) {
  4. var arrIndexes = new Array;
  5.  
  6. for (var i=0; i < oListbox.options.length; i++) {
  7. if (oListbox.options[i].selected) {
  8. arrIndexes.push(i);
  9. }
  10. }
  11.  
  12. return arrIndexes;
  13. };
  14.  
  15. ListUtil.add = function (oListbox, sName, sValue) {
  16.  
  17. var oOption = document.createElement("option");
  18. oOption.appendChild(document.createTextNode(sName));
  19.  
  20. if (arguments.length == 3) {
  21. oOption.setAttribute("value", sValue);
  22. }
  23.  
  24. oListbox.appendChild(oOption);
  25.  
  26. }
  27.  
  28. ListUtil.remove = function (oListbox, iIndex) {
  29. oListbox.remove(iIndex);
  30. };
  31.  
  32. ListUtil.clear = function (oListbox) {
  33. for (var i=oListbox.options.length-1; i >= 0; i--) {
  34. ListUtil.remove(oListbox, i);
  35. }
  36. };
  37.  
  38. ListUtil.move = function (oListboxFrom, oListboxTo, iIndex) {
  39. var oOption = oListboxFrom.options[iIndex];
  40.  
  41. if (oOption != null) {
  42. oListboxTo.appendChild(oOption);
  43. }
  44. };
  45.  
  46. ListUtil.shiftUp = function (oListbox, iIndex) {
  47. if (iIndex > 0) {
  48. var oOption = oListbox.options[iIndex];
  49. var oPrevOption = oListbox.options[iIndex-1];
  50. oListbox.insertBefore(oOption, oPrevOption);
  51. }
  52. };
  53.  
  54. ListUtil.shiftDown = function (oListbox, iIndex) {
  55. if (iIndex < oListbox.options.length - 1) {
  56. var oOption = oListbox.options[iIndex];
  57. var oNextOption = oListbox.options[iIndex+1];
  58. oListbox.insertBefore(oNextOption, oOption);
  59. }
  60. };

Report this snippet  

You need to login to post a comment.