bootstrap-number-input.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 setText(n) {
  34. if ((min && n < min) || (max && n > max)) {
  35. return false;
  36. }
  37. clone.focus().val(n);
  38. return true;
  39. }
  40. var group = $("<div class='input-group'></div>");
  41. var down = $("<button type='button'>-</button>").attr('class', 'btn btn-' + settings.downClass).click(function () {
  42. setText(parseInt(clone.val()) - 1);
  43. });
  44. var up = $("<button type='button'>+</button>").attr('class', 'btn btn-' + settings.upClass).click(function () {
  45. setText(parseInt(clone.val()) + 1);
  46. });
  47. $("<span class='input-group-btn'></span>").append(down).appendTo(group);
  48. clone.appendTo(group);
  49. if (clone) {
  50. clone.css('text-align', 'center');
  51. }
  52. $("<span class='input-group-btn'></span>").append(up).appendTo(group);
  53. // remove spins from original
  54. clone.prop('type', 'text').keydown(function (e) {
  55. if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||
  56. (e.keyCode == 65 && e.ctrlKey === true) ||
  57. (e.keyCode >= 35 && e.keyCode <= 39)) {
  58. return;
  59. }
  60. if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
  61. e.preventDefault();
  62. }
  63. var c = String.fromCharCode(e.which);
  64. var n = parseInt(clone.val() + c);
  65. //if ((min && n < min) || (max && n > max)) {
  66. // e.preventDefault();
  67. //}
  68. });
  69. clone.prop('type', 'text').blur(function (e) {
  70. var c = String.fromCharCode(e.which);
  71. var n = parseInt(clone.val() + c);
  72. if ((min && n < min)) {
  73. setText(min);
  74. }
  75. else if (max && n > max) {
  76. setText(max);
  77. }
  78. });
  79. self.replaceWith(group);
  80. });
  81. };
  82. }(jQuery));