jeudi 19 mars 2015

Change textbox value on checkbox select

What I have now, the output default value is 10 and when I select option there should sum the selected value with output. For example my output is 10 and I select First option (8), then my out put should be 18, but not 108 as it is now.


http://ift.tt/1H4I0eN



<input type="text" class="output" value="10" />
<br>

<input type="checkbox" class="qwer" value="8">First option
<br>
<input type="checkbox" class="numbers" value="8">Second option
<br>
<input type="checkbox" class="asdfg" value="15">Third option



$('input.qwer').on('change', function () {
if ($(this).is(':checked')) {
$('.output').val($('.output').val() + $(this).val());
} else {
currentVal = $('.output').val();
currentVal = currentVal.replace($('.qwer').val(), '');
$('.output').val(currentVal);
}
});

$('input.numbers').on('change', function () {
if ($(this).is(':checked')) {
$('.output').val($('.output').val() + $(this).val());
} else {
currentVal = $('.output').val();
currentVal = currentVal.replace($('.numbers').val(), '');
$('.output').val(currentVal);
}
});

$('input.asdfg').on('change', function () {
if ($(this).is(':checked')) {
$('.output').val($('.output').val() + $(this).val());
} else {
currentVal = $('.output').val();
currentVal = currentVal.replace($('.asdfg').val(), '');
$('.output').val(currentVal);
}
});




Aucun commentaire:

Enregistrer un commentaire