Yii2-dynamicforms和javascript

我有在Yii2中工作的wbraganca动态表单,但我需要添加一个函数来乘以2个字段并将值放在第三个中,并在每个新动态表单中执行此操作(假设字段1是价格,字段2是金额和字段3总计).
使用我的代码,我能够做到这一点,但只能在第一个动态表单上.
<?PHP
$script = <<< JS
$('#itemsfactura-{$i}-cantidad').change(function(){
    var cantidad = $(this).val();
    var precio = $('#itemsfactura-{$i}-precio').val();
    $('#itemsfactura-{$i}-total_item').val(cantidad * precio);
});
JS;
$this->registerJs($script);
?>

这是动态表单字段的代码

...
<div class="row">
    <div class="col-sm-4">
        <?= $form->field($modelItemFactura,"[{$i}]precio")->textInput(['maxlength' => true]) ?>
    </div>
    <div class="col-sm-4">
        <?= $form->field($modelItemFactura,"[{$i}]cantidad")->textInput(['maxlength' => true]) ?>
    </div>
    <div class="col-sm-4">
        <?= $form->field($modelItemFactura,"[{$i}]total_item")->textInput(['maxlength' => true]) ?>
    </div>
</div>...
形成
<div class="col-sm-4">
    <?= $form->field($modelItemFactura,"[{$i}]precio")->textInput(['maxlength' => true,'onchange' => 'getProduct($(this))','onkeyup' => 'getProduct($(this))']) ?>
</div>
<div class="col-sm-4">
    <?= $form->field($modelItemFactura,"[{$i}]cantidad")->textInput(['maxlength' => true,'onkeyup' => 'getProduct($(this))']) ?>
</div>

JS

function getProduct(item) {
    var index  = item.attr("id").replace(/[^0-9.]/g,"");
    var total = current = next = 0;

    var id = item.attr("id");
    var myString = id.split("-").pop();

    if(myString == "precio") {
        fetch = index.concat("-cantidad");
    } else {
        fetch = index.concat("-precio");
    }

    temp = $("#itemsfactura-"+fetch+"").val();

    if(!isNaN(temp) && temp.length != 0) {
        next = temp;
    }

    current = item.val();
    if(isNaN(current) || current.length == 0) {
        current = 0;
    }

    if(!isNaN(current) && !isNaN(next)) {
        total = parseInt(current) * parseInt(next);
    }

    totalItem = "itemsfactura-".concat(index).concat("-total_item");

    $("#"+totalItem+"").val(total);
}

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...