我正在尝试将两个文本框(qty,rate)的产品从模型productsales获得到第三个文本框(value),其动态形式为yii2。它与Yii2-dynamicforms and javascript完全相同,除了第3个文本框不属于任何模型。 _form
<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))','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 ">
<input type="text" class="form-control" id="value">
</div>
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 = parseInt(current) * parseInt(next);
}
vALUE = "productsales-".concat(index).concat("-value");
$("#"+vALUE+"").val(value);
}
JS;
$this->registerJs($script, View::POS_END);
/* end getting the product value */
?>
这不是任何输出。如果需要任何额外的代码,请告诉我。
答案 0 :(得分:1)
<强> _form.php这个强>
<input type="text" class="form-control" id="productsales-<?= $i ?>-value">
<强> JS 强>
if(!isNaN(current) && !isNaN(next)) {
total = parseInt(current) * parseInt(next);
}
valueField = "productsales-".concat(index).concat("-value");
$("#"+valueField+"").val(total);
答案 1 :(得分:0)
<?= $form->field($model, 'quantity')->textInput(['id' => "quantity"]) ?>
<?= $form->field($model, 'subtotal')->textInput(['id' => "price"]) ?>
<input type="text" id="total">
<?php
$script = <<<EOD
$(function() {
$('#quantity').keyup(function() {
updateTotal();
});
$('#price').keyup(function() {
updateTotal();
});
var updateTotal = function() {
var quantity = parseInt($('#quantity').val());
var price = parseInt($('#price').val());
var total = quantity * price;
$('#total').val(total);
};
});
EOD;
$this->registerJs($script);
?>