将计算数据传递到texbox中,而不是以动态形式yii2从任何模型传递

时间:2016-11-11 22:36:33

标签: yii2

我正在尝试将两个文本框(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 */
?>

这不是任何输出。如果需要任何额外的代码,请告诉我。

2 个答案:

答案 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);
?>