Calculate from 3 inputfield in dynamic form yii2

前端 未结 1 806
深忆病人
深忆病人 2021-01-28 23:39

In my dynamic form I need to calculate data from from 3 fields(qty,rate,discount) and pass it to unitdiscount. The formula will be - unitdiscount = ((qty*rate*discount)/10

相关标签:
1条回答
  • Try this way :

    function getUdisc(item) {
        var index  = item.attr("id").replace(/[^0-9.]/g, "");
        var total = current = next = previous = 0;
    
        var id = item.attr("id");
        var myString = id.split("-").pop();
    
        if (myString == "qty") {
            fetch1 = index.concat("-discount");
            fetch2 = index.concat("-rate");
        } else if (myString == "discount") {
            fetch1 = index.concat("-qty");
            fetch2 = index.concat("-rate");
        } else {
            fetch1 = index.concat("-discount");
            fetch2 = index.concat("-qty");
        }
    
        temp1 = $("#productsales-"+fetch1+"").val();
        temp2 = $("#productsales-"+fetch2+"").val();
    
        if (!isNaN(temp1) && temp1.length != 0) {
            next = temp1;
        }
    
        if (isNaN(temp2) || temp2.length == 0) {
            previous = temp2;
        }
    
        current = item.val();
        if (isNaN(current) || current.length == 0) {
            current = 0;
        }
    
        if (!isNaN(current) && !isNaN(next) && !isNaN(previous)) {
            total = (parseFloat(current) + parseFloat(next) + parseFloat(previous)).toFixed(2);
        }
    
        udiscField = "productsales-".concat(index).concat("-udisc");
    
        $("#"+udiscField+"").val(total);
    }
    
    0 讨论(0)
提交回复
热议问题