使用jQuery更改动态创建的输入字段的值

时间:2015-06-20 16:09:45

标签: javascript jquery html

请查看以下jsfiddle;

https://jsfiddle.net/1kvwxmyb/3/

基本上,代码允许用户在发票中添加“行”。当用户选择产品的“增值税税率”时,我需要使用jQuery计算“增值税金额”和“总金额”。计算这些数字是基本的数学,但目前我甚至不能让jQuery在'VAT Amount'和'Gross Amount'中设置任何类型的值,更不用说正确的数字了!

请帮忙。

的jQuery

// calculate VAT amount and set gross amount

$(".vat_rate").change(function () {

    alert("Handler for .change() called.");

    $(this).nextAll('input').first().val("123");

});

4 个答案:

答案 0 :(得分:2)

因此,首先,请将相关代码更改为以下内容。

// calculate VAT amount and set gross amount
$(".nobord").on("change", ".vat_rate", function () {

    var $row = $(this).closest("tr");

    var $qty = $row.find("input[name^='invoice_line_quantity']");
    var $amount = $row.find("input[name^='invoice_line_amount']");
    //Value of the Qty input
    console.log($qty.val());
    //Value of the Net Amount input
    console.log($amount.val());

    //Now do your math and set the value as below

    $row.find("input[name^='invoice_line_vat_amount']").val("your_value");
    $row.find("input[name^='invoice_line_gross_amount']").val("your_value");
});

由于关闭TD / TR的顺序不正确,你的HTML有点搞砸了。看看我所做的更正on this fiddle

希望这是一个好的开始:)

答案 1 :(得分:0)

我更新了你的小提琴Python documentation

刚刚将clone参数添加到change函数,这会导致带有事件处理程序的clone元素。

来自jQuery文档的引用

  

.clone([withDataAndEvents])

     

withDataAndEvents(默认值:false)

     

类型:布尔值

     

一个布尔值,指示是否应将事件处理程序与元素一起复制。从jQuery 1.4开始,元素数据也将被复制。

我还将样本计算添加到var $row = $(this).closest("tr");函数中。

要获取选项已更改的行$row.find("input[name^='invoice_line_vat_amount']"),然后您可以找到remID的输入

答案 2 :(得分:0)

已修复,现在它适用于新行https://jsfiddle.net/1kvwxmyb/11/

JS

    $(document).on('change', '.vat_rate',function () {

        var row = $(this).parent().parent().attr("id");

        var qty = $("#" + row + " td:nth-child(1) input");
        var desc = $("#" + row + " td:nth-child(2) input");
        var netam = $("#" + row + " td:nth-child(3) input");
        var vatam = $("#" + row + " td:nth-child(5) input");
        var grossam = $("#" + row + " td:nth-child(6) input");

        vatam.val("123");

        //alert("Handler for .change() called.");

        //$(this).nextAll('input').first().val("123");

    });

答案 3 :(得分:0)

一点改变

// calculate VAT amount and set gross amount

$(".vat_rate").change(function (event) {

    $(event.target).parent().parent().find('td:nth-child(3) input').val('0.5');

});