【问题标题】:Filling the another input field based on the selection of check box根据复选框的选择填充另一个输入字段
【发布时间】:2017-10-12 18:11:22
【问题描述】:

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

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

【问题讨论】:

    标签: jquery


    【解决方案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>
    

    【讨论】:

    • 这段代码的一个问题是,如果我取消选中一个复选框,所有值都会消失。它应该只减去我未选中的值。你能帮我吗..
    • 你能提供你的 html 的外观吗
    猜你喜欢
    • 1970-01-01
    • 2011-08-17
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多