Skip to content

Instantly share code, notes, and snippets.

@maksimerohin
Last active December 8, 2018 02:55
Show Gist options
  • Save maksimerohin/edf0ae0ec9b6df9f15e325bf3eda22f6 to your computer and use it in GitHub Desktop.
Save maksimerohin/edf0ae0ec9b6df9f15e325bf3eda22f6 to your computer and use it in GitHub Desktop.
Кнопки +/- на формах
<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">&minus;</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>
$('.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