I got a form, here's the jQuery code and HTML. As you can see, the form has 8 input fields which are divided into 4 pairs. Every input can have only values from 0 to 4. How can I achieve this behavior:
If one of the input fields has the value 4, the second input field in the pair can have max value of 3.
jQuery(document).ready(function(){
// This button will increment the value
$('.qtyplus').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('input[name='+fieldName+']').val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(0);
}
if((currentVal) > 2) {
$('input[field='+fieldName+'].qtyplus').attr("disabled", true);
}
});
// This button will decrement the value till 0
$(".qtyminus").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('input[name='+fieldName+']').val());
// If it isn't undefined or its greater than 0
if (!isNaN(currentVal) && currentVal > 0) {
// Decrement one
$('input[name='+fieldName+']').val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('input[name='+fieldName+']').val(0);
}
if((currentVal) <= 4) {
$('input[field='+fieldName+'].qtyplus').attr("disabled", false);
}
});
});
<input type='button' value='-' class='qtyminus' field='quantity' />
<input type='text' name='quantity' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity'/><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity2' />
<input type='text' name='quantity2' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity2' />
<br/><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity3' />
<input type='text' name='quantity3' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity3' /><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity4' />
<input type='text' name='quantity4' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity4' />
<br/><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity5' />
<input type='text' name='quantity5' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity5' /><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity6' />
<input type='text' name='quantity6' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity6' />
<br/><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity7' />
<input type='text' name='quantity7' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity7' /><br/><br/>
<input type='button' value='-' class='qtyminus' field='quantity8' />
<input type='text' name='quantity8' value='0' class='qty' readonly />
<input type='button' value='+' class='qtyplus' field='quantity8' />