Last active
December 8, 2018 02:55
-
-
Save maksimerohin/edf0ae0ec9b6df9f15e325bf3eda22f6 to your computer and use it in GitHub Desktop.
Кнопки +/- на формах
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="input-group input-group"> | |
<div class="input-group-prepend"> | |
<button class="btn btn-secondary js-num" type="button" data-type="minus" data-field="input-one">−</button> | |
</div> | |
<input type="text" min="0" max="100" step="5" value="1" id="input-one" class="form-control"> | |
<div class="input-group-append"> | |
<button class="btn btn-secondary js-num" type="button" data-type="plus" data-field="input-one">+</button> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.js-num').on('click', (function() { | |
var type = $(this).attr('data-type'); | |
var field = $(this).attr('data-field'); | |
// var input = $('input[name ='+ field +']'); | |
var input = $('input#' + field); | |
var min = input.attr('min'); | |
var max = input.attr('max'); | |
var step = input.attr('step'); | |
min =parseInt(min); | |
max =parseInt(max); | |
step =parseInt(step); | |
var value = input.val(); | |
var currentVal = value; | |
if(type == 'minus') | |
{ | |
if(value>min) | |
{ | |
currentVal = parseInt(value) - step; | |
input.val(currentVal).change(); | |
} | |
} | |
if(type == 'plus') | |
{ | |
if(value<max) | |
{ | |
currentVal = parseInt(value) + step; | |
input.val(currentVal).change(); | |
} | |
} | |
$('input#' + field).val(currentVal); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment