I just want to auto fill input fields based on the selection of checkbox. Below is the code i am using. Its working fine but the problem is that i want to fill the input field only if the user checked the checkbox. if the user unchecked the check box again i want to remove the values that filled by the selection.
<script>
$('.dtgPaymentDetails').change(function () {
var flag = false;
var receiptid = $(this).attr("receiptid");
var receiptamount = $(this).attr("receiptamt");
var totalamount = $("#txtTotatAmount").val();
if (isNaN(parseInt(totalamount)))
{
totalamount = 0;
}
totalamount = parseInt(totalamount) + parseInt(receiptamount);
$("#txtTotatAmount").val(totalamount);
$("#txtNetAmount").val(totalamount);
});
</script>
答案 0 :(得分:1)
You need to check value is checked are not.
<script>
$('.dtgPaymentDetails').change(function () {
if($(this).is(":checked")) {
var flag = false;
var receiptid = $(this).attr("receiptid");
var receiptamount = $(this).attr("receiptamt");
var totalamount = $("#txtTotatAmount").val();
if (isNaN(parseInt(totalamount)))
{
totalamount = 0;
}
totalamount = parseInt(totalamount) + parseInt(receiptamount);
$("#txtTotatAmount").val(totalamount);
$("#txtNetAmount").val(totalamount);
} else {
$("#txtTotatAmount").val("");
$("#txtNetAmount").val("");
}
});
</script>