bootstrap-number-input.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* ========================================================================
  2. * bootstrap-spin - v1.0
  3. * https://github.com/wpic/bootstrap-spin
  4. * ========================================================================
  5. * Copyright 2014 WPIC, Hamed Abdollahpour
  6. *
  7. * ========================================================================
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ========================================================================
  20. */
  21. (function ($) {
  22. $.fn.bootstrapNumber = function (options) {
  23. var settings = $.extend({
  24. upClass: 'default',
  25. downClass: 'default',
  26. center: true
  27. }, options);
  28. return this.each(function (e) {
  29. var self = $(this);
  30. var clone = self.clone();
  31. var min = self.attr('min');
  32. var max = self.attr('max');
  33. function getVal() {
  34. var val = clone.val();
  35. if (! val || val === "NaN") {
  36. return 0;
  37. }
  38. return parseInt(val);
  39. }
  40. function setText(n) {
  41. if ((min && n < min) || (max && n > max)) {
  42. return false;
  43. }
  44. clone.focus().val(n);
  45. return true;
  46. }
  47. var group = $("<div class='input-group'></div>");
  48. var down = $("<button type='button'>-</button>").attr('class', 'btn btn-' + settings.downClass).click(function () {
  49. setText(getVal() - 1);
  50. });
  51. var up = $("<button type='button'>+</button>").attr('class', 'btn btn-' + settings.upClass).click(function () {
  52. setText(getVal() + 1);
  53. });
  54. $("<span class='input-group-btn'></span>").append(down).appendTo(group);
  55. clone.appendTo(group);
  56. if (clone) {
  57. clone.css('text-align', 'center');
  58. }
  59. $("<span class='input-group-btn'></span>").append(up).appendTo(group);
  60. // remove spins from original
  61. clone.prop('type', 'text').keydown(function (e) {
  62. if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
  63. (e.keyCode == 65 && e.ctrlKey === true) ||
  64. (e.keyCode >= 35 && e.keyCode <= 39)) {
  65. return;
  66. }
  67. if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
  68. e.preventDefault();
  69. }
  70. var c = String.fromCharCode(e.which);
  71. var n = (getVal() + c);
  72. //if ((min && n < min) || (max && n > max)) {
  73. // e.preventDefault();
  74. //}
  75. });
  76. clone.prop('type', 'text').blur(function (e) {
  77. var c = String.fromCharCode(e.which);
  78. var n = getVal() + c;
  79. if ((min && n < min)) {
  80. setText(min);
  81. }
  82. else if (max && n > max) {
  83. setText(max);
  84. }
  85. });
  86. self.replaceWith(group);
  87. });
  88. };
  89. }(jQuery));