太多的递归 - 单选按钮 - jquery

时间:2016-11-27 10:34:08

标签: javascript jquery yii2

我有一个动态表单,在那个很多字段中,我希望在用户继续使用表单时进行自动计算。在表单中,我有2个单选按钮Button b = (Button) rootView.findViewById(R.id.mybtn); mrp。默认情况下,选择mrp。如果用户未更改所选的默认按钮,则值rate将计算为所有行的totalCst。 如果用户选择qty*mrp*cstpercent radiobutton,则该值将计算为所有行的rate。我稍后会添加onchange事件。首先,我想获得仅在mrp上计算的值totalCst,或者根据所选择的值进行评分。

单选按钮代码 -

qty*rate*cstpercent

JS

<div class="form-group pull-right">
                    <label class="radio-inline"><input type="radio" name="taxon" id="mrp"  value="mrp" checked="checked">MRP</label>
                    <label class="radio-inline"><input type="radio" name="taxon" id="rate" value="rate">Rate</label>
                </div>

我在警报中选择了值,但是弹出太多并且收到错误 - 递归太多了 enter image description here

更新了JS

<?php
/* start getting the total amount */
$this->registerJs('
    function getSum() {
        var sum = 0;
        var totalDiscount = 0;
        var totalMrp = 0;
        var totalCst = 0;
        var totalWbst = 0;
        var totalCstonamount = 0;
        var totalWbstonamount = 0;
        var totalCstonmrp = 0;
        var totalWbstonmrp = 0;

        var cstperValue = $(".cstPercent").val();
        var wbstperValue = $(".wbstPercent").val();
        var selectedValue = $("input[name=taxon]:checked").val();
        alert(selectedValue);

        var items = $(".item");
        items.each(function (index, elem) {
            var qtyValue = $(elem).find(".qty").val();
            var rateValue = $(elem).find(".rate").val();
            var discValue = $(elem).find(".disc").val();
            var mrpValue = $(elem).find(".mrp").val();
            var freeValue = $(elem).find(".free").val();


            sum = (parseFloat(sum) + (parseFloat(qtyValue) * parseFloat(rateValue))).toFixed(2);
            totalDiscount = (parseFloat(totalDiscount) + ((parseFloat(qtyValue) * parseFloat(rateValue) * parseFloat(discValue))/100)).toFixed(2);
            totalMrp = (parseFloat(totalMrp) + ((parseFloat(qtyValue) + parseFloat(freeValue)) * parseFloat(mrpValue))).toFixed(2);
            totalCstonamount = (parseFloat(totalCst) + ((parseFloat(qtyValue) * parseFloat(rateValue)) * parseFloat(cstperValue))/100).toFixed(2);
            totalWbstonamount = (parseFloat(totalWbst) + ((parseFloat(qtyValue) * parseFloat(rateValue)) * parseFloat(wbstperValue))/100).toFixed(2);
            totalCstonmrp = (parseFloat(totalCst) + (((parseFloat(qtyValue) + parseFloat(freeValue)) * parseFloat(mrpValue)) * parseFloat(cstperValue))/100).toFixed(2);
            totalWbstonmrp = (parseFloat(totalWbst) + (((parseFloat(qtyValue) + parseFloat(freeValue)) * parseFloat(mrpValue)) * parseFloat(wbstperValue))/100).toFixed(2);



        });

       if(isNaN(sum) || sum.length == 0) {
           sum = 0;
       }
       if(isNaN(totalDiscount) || totalDiscount.length == 0) {
           totalDiscount = 0;
       }
       if(isNaN(totalMrp) || totalMrp.length == 0) {
           totalMrp = 0;
       }
       if(isNaN(totalCstonamount) || totalCstonamount.length == 0) {
           totalCstonamount = 0;
       }
       if(isNaN(totalWbstonamount) || totalWbstonamount.length == 0) {
           totalWbstonamount = 0;
       }
       if(isNaN(totalCstonmrp) || totalCstonmrp.length == 0) {
           totalCstonmrp = 0;
       }
       if(isNaN(totalWbstonmrp) || totalWbstonmrp.length == 0) {
           totalWbstonmrp = 0;
       }



       $(".sum").val(sum);
       $(".totalDiscount").val(totalDiscount);
       $(".totalMrp").val(totalMrp);


       if (selectedValue == "mrp") {
            getSum();
           $(".totalCst").val(totalCstonmrp);
        }
        else if (selectedValue == "rate") {
            getSum();
           $(".totalCst").val(totalCstonamount);
        }

    }

    $(".container-items").on("change", function() {
        getSum();
    });

    $(".cstPercent").on("change", function() {
        getSum();
    });

    $(".wbstPercent").on("change", function() {
        getSum();
    });

    jQuery(".dynamicform_wrapper").on("afterDelete", function(e) {
        getSum();
    }); 

');
/*end getting the total amount */
?>

根据我的理解,现在的问题是将getSum函数内部的计算值提取到它之外。

1 个答案:

答案 0 :(得分:0)

  

首先,我想获得仅在mrp或rate上计算的值totalCst   无论选择哪一个。

您可以在此处使用三元运算符,而不是if... else...

var factor = (factorMrp.checked ? 'mrp' : 'rate');

工作示例:

&#13;
&#13;
var factors = document.getElementById('factors');
var factorMrp = document.getElementById('mrp');
var factorRate = document.getElementById('rate');
var paragraph = document.getElementsByTagName('p')[0];

factorMrp.checked = true;

function checkFactor() {
var factor = (factorMrp.checked ? 'mrp' : 'rate');
paragraph.textContent = 'The value totalCst will be calculated as qty * ' + factor + ' * cstpercent of all the rows.';
}

factors.addEventListener('change',checkFactor,false);
window.addEventListener('load',checkFactor,false);
&#13;
fieldset {
display: inline-block;
}
&#13;
<form>
<fieldset id="factors">
<label class="radio-inline"><input type="radio" name="taxon" id="mrp"  value="mrp" checked="checked">MRP</label>
<label class="radio-inline"><input type="radio" name="taxon" id="rate" value="rate">Rate</label>
</fieldset>
</form>

<p></p>
&#13;
&#13;
&#13;