bootstrap-number-input.js 3.4 KB

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