2017-10-12 95 views
0

我只想根据复选框的选择自动填充输入字段。 以下是我正在使用的代码。它的工作正常,但问题是,我想只填写输入字段,如果用户选中复选框。如果用户再次取消选中复选框,我想删除由选择填充的值。根据复选框的选择填充另一个输入字段

<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> 

回答

1

您需要检查的值是否被检查不是。

<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> 
+0

非常感谢。我的工作.... – Suthar

+0

这个代码的一个问题是,如果我没有选中一个复选框,所有的值将消失。它应该只减去我未选中的值。你能帮我解决这个问题吗? – Suthar

+0

你能提供一下你的html的外观吗? –

相关问题