A-A+

jquery select操作代码

2016年01月07日 前端设计 暂无评论 阅读 6 views 次

这里包括jquery获取 select项的个数,索引,前选中项的文本 选中项的值 text的第一项被选中 删除select中选定的项 等操作。

  1. //得到select项的个数   
  2. jquery.fn.size = function(){   
  3. return jquery(this).get(0).options.length;   
  4. }  
  5. //获得选中项的索引   
  6. jquery.fn.getselectedindex = function(){   
  7. return jquery(this).get(0).selectedindex;   
  8. }  
  9. //获得当前选中项的文本   
  10. jquery.fn.getselectedtext = function(){   
  11. if(this.size() == 0) return "下拉框中无选项";   
  12. else{   
  13. var index = this.getselectedindex();   
  14. return jquery(this).get(0).options[index].text;   
  15. }   
  16. }  
  17. //获得当前选中项的值   
  18. jquery.fn.getselectedvalue = function(){   
  19. if(this.size() == 0)   
  20. return "下拉框中无选中值";  
  21. else   
  22. return jquery(this).val();   
  23. }  
  24. //设置select中值为value的项为选中   
  25. jquery.fn.setselectedvalue = function(value){   
  26. jquery(this).get(0).value = value;   
  27. }  
  28. //设置select中文本为text的第一项被选中   
  29. jquery.fn.setselectedtext = function(text)   
  30. {   
  31. var isexist = false;   
  32. var count = this.size();   
  33. for(var i=0;i<count;i++)   
  34. {   
  35. if(jquery(this).get(0).options[i].text == text)   
  36. {   
  37. jquery(this).get(0).options[i].selected = true;   
  38. isexist = true;   
  39. break;   
  40. }   
  41. }   
  42. if(!isexist)   
  43. {   
  44. alert("下拉框中不存在该项");   
  45. }   
  46. }   
  47. //设置选中指定索引项   
  48. jquery.fn.setselectedindex = function(index)   
  49. {   
  50. var count = this.size();   
  51. if(index >= count || index < 0)   
  52. {   
  53. alert("选中项索引超出范围");   
  54. }   
  55. else   
  56. {   
  57. jquery(this).get(0).selectedindex = index;   
  58. }   
  59. }   
  60. //判断select项中是否存在值为value的项   
  61. jquery.fn.isexistitem = function(value)   
  62. {   
  63. var isexist = false;   
  64. var count = this.size();   
  65. for(var i=0;i<count;i++)   
  66. {   
  67. if(jquery(this).get(0).options[i].value == value)   
  68. {   
  69. isexist = true;   
  70. break;   
  71. }   
  72. }   
  73. return isexist;   
  74. }   
  75. //向select中添加一项,显示内容为text,值为value,如果该项值已存在,则提示   
  76. jquery.fn.addoption = function(text,value)   
  77. {   
  78. if(this.isexistitem(value))   
  79. {   
  80. alert("待添加项的值已存在");   
  81. }   
  82. else   
  83. {   
  84. jquery(this).get(0).options.add(new option(text,value));   
  85. }   
  86. }   
  87. //删除select中值为value的项,如果该项不存在,则提示   
  88. jquery.fn.removeitem = function(value)   
  89. {   
  90. if(this.isexistitem(value))   
  91. {   
  92. var count = this.size();   
  93. for(var i=0;i<count;i++)   
  94. {   
  95. if(jquery(this).get(0).options[i].value == value)   
  96. {   
  97. jquery(this).get(0).remove(i);   
  98. break;   
  99. }   
  100. }   
  101. }   
  102. else   
  103. {   
  104. alert("待删除的项不存在!");   
  105. }   
  106. }   
  107. //删除select中指定索引的项   
  108. jquery.fn.removeindex = function(index)   
  109. {   
  110. var count = this.size();   
  111. if(index >= count || index < 0)   
  112. {   
  113. alert("待删除项索引超出范围");   
  114. }   
  115. else   
  116. {   
  117. jquery(this).get(0).remove(index);   
  118. }   
  119. }   
  120. //删除select中选定的项   
  121. jquery.fn.removeselected = function()   
  122. {   
  123. var index = this.getselectedindex();   
  124. this.removeindex(index);   
  125. }   
  126. //清除select中的所有项   
  127. jquery.fn.clearall = function()   
  128. {   
  129. jquery(this).get(0).options.length = 0;   
  130. }  
标签:

给我留言