在我的动态表单中,我需要计算来自3个字段(数量,费率,折扣)的数据并将其传递给unitdiscount。公式为 - unitdiscount = ((qty*rate*discount)/100)
。当我将getValue
传递给(qty*rate)
时,我已经对输入字段数量和速率进行了onchange事件(value
)。当我添加新的onchange(getUnitdiscount
)时,不会传递计算的字段value
。相反,我得到unitdiscount
。另外,不确定如何从3个inputdield计算。
我在Pass calculated data in a texbox not from any model in dynamic form yii2
的帮助下计算了value
我现在的代码看起来像 -
_form
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]rate")->label(false)->textInput(['maxlength' => true,'onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Rate']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]qty")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','onchange' => 'getValue($(this))', 'onkeyup' => 'getValue($(this))','onchange' => 'getUnitdiscount($(this))', 'onkeyup' => 'getUnitdiscount($(this))','placeholder' => 'Qty']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]free")->label(false)->textInput(['maxlength' => true,'onchange' => 'getTotal($(this))', 'onkeyup' => 'getTotal($(this))','placeholder' => 'Free']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 nopadding">
<?= $form->field($modelsProductsales, "[{$i}]discount")->label(false)->textInput(['maxlength' => true,'placeholder' => 'Discount']) ?>
</div>
<div class="col-xs-1 col-sm-1 col-lg-1 ">
<input type="text" class="form-control" id="productsales-<?= $i ?>-value">
</div>
<input type="text" class="form-control" id="productsales-<?= $i ?>-unitdiscount">
JS功能
<?php
/* start getting the product value */
$script = <<< JS
function getValue(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 == "qty") {
fetch = index.concat("-rate");
} else {
fetch = index.concat("-qty");
}
temp = $("#productsales-"+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 =parseFloat((parseFloat(current) * parseFloat(next))).toFixed(2);
}
valueField = "productsales-".concat(index).concat("-value");
$("#"+valueField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product value */
?>
<?php
/* start getting the product Unit discount */
$script = <<< JS
function getUnitdiscount(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 == "qty") {
fetch = index.concat("-rate");
} else {
fetch = index.concat("-qty");
}
temp = $("#productsales-"+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 =parseFloat((parseFloat(current) * parseFloat(next))).toFixed(2);
}
unitdiscountField = "productsales-".concat(index).concat("-unitdiscount");
$("#"+unitdiscountField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product Unit discount */
?>
更新 -
我已经更改了inptfields并将onchange(getUnitdiscount)
放在onchange(getUnitdiscount
)前面。现在我至少在文本框unitdiscount
和value
中获得了输出。
我尝试使用以下javascript,但没有给出正确的结果。
<?php
/* start getting the total udisc */
$script = <<< JS
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") {
fetch2 = index.concat("-discount");
fetch1 = index.concat("-rate");
} else if(myString == "discount") {
fetch3 = index.concat("-qty");
fetch1 = index.concat("-rate");
} else {
fetch2 = index.concat("-discount");
fetch3 = index.concat("-qty");
}
temp = $("#productsales-"+fetch1+"").val();
if(!isNaN(temp) && temp.length != 0) {
next = temp;
}
current = item.val();
if(isNaN(current) || current.length == 0) {
current = 0;
}
previous = item.val();
if(isNaN(previous) || previous.length == 0) {
previous = 0;
}
if(!isNaN(current) && !isNaN(next) && !isNaN(previous)) {
total = parseFloat(current) + parseFloat(next) + parseFloat(previous);
}
udiscField = "productsales-".concat(index).concat("-udisc");
$("#"+udiscField+"").val(total);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the total udisc */
?>
如果我将rate = 50.50
,qty = 100
,discount = 10
放入此javascript,则应该提供结果160.50
,但它会给70.50
。 (我使用了一个简单的公式unitdiscount = rate + qty + discount
来测试我是否正确获取了值,然后我可以将公式更改为复杂公式。)
答案 0 :(得分:1)
尝试这种方式:
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);
}