Yii2-dynamicforms and javascript

后端 未结 1 1969
野性不改
野性不改 2020-12-20 06:16

I have wbraganca dynamic forms working in Yii2, but I need to add a little function to multiply 2 fields and put value in a third, and do that in every new dynamic form (sup

相关标签:
1条回答
  • 2020-12-20 06:56

    Form

    <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, 'onchange' => 'getProduct($(this))', '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);
    }
    
    0 讨论(0)
提交回复
热议问题