jquery.vselect.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Vue-jQuery Select插件
  3. * 为防止Vue.js与jQuery冲突,该插件未使用Vue.js
  4. *
  5. * author
  6. * 2016-01-05
  7. */
  8. ;(function($) {
  9. $.fn.extend({
  10. 'vselect': function(args) {
  11. // 默认设置
  12. var defaults = {
  13. options: [],
  14. selected: 0,
  15. onSelect: function(value, index) {
  16. console.log(value, index);
  17. },
  18. style: 'warning',
  19. maxheight: '640px',
  20. btnWidth: '100%',
  21. btnSize: 'xl',
  22. }
  23. if (typeof(vselect_options) == 'undefined') {
  24. vselect_options = $.extend(defaults, args);
  25. } else {
  26. vselect_options = $.extend(vselect_options, args);
  27. }
  28. // 内部方法
  29. vselect_privateFunction = {
  30. selected: function(value, index) {
  31. vselect_options.onSelect(value, index);
  32. }
  33. }
  34. // 生成选择框
  35. var _html = '<select class="v_select">';
  36. var options = vselect_options.options;
  37. for(key in options) {
  38. if (key == vselect_options.selected) {
  39. _html += '<option value="'+options[key]['value']+'" index="'+key+'" selected>';
  40. } else {
  41. _html += '<option value="'+options[key]['value']+'" index="'+key+'">';
  42. }
  43. _html += options[key]['text'];
  44. _html += '</option>';
  45. }
  46. _html += '</select>';
  47. this.html(_html);
  48. // 改变select值时的方法
  49. this.find('.v_select').change(function() {
  50. var value = $(this).val();
  51. var index = $(this).children('option[value="'+value+'"]').index();
  52. vselect_privateFunction.selected(value, index);
  53. });
  54. // AmazeUI
  55. this.find('.v_select').selected({
  56. btnWidth: vselect_options.btnWidth,
  57. btnSize: vselect_options.btnSize,
  58. btnStyle: vselect_options.style,
  59. maxHeight: vselect_options.maxHeight
  60. });
  61. },
  62. 'getVIndex' : function() {
  63. if (typeof(vselect_options) != 'undefined') {
  64. return vselect_options.selected;
  65. } else {
  66. return 0;
  67. }
  68. },
  69. 'setVIndex' : function(val) {
  70. if (typeof(vselect_options) != 'undefined') {
  71. return vselect_options.selected = val;
  72. } else {
  73. return false;
  74. }
  75. }
  76. });
  77. })(jQuery);