PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Yii2-dynamicforms和javascript大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有在Yii2中工作的wbraganca动态表单,但我需要添加一个函数来乘以2个字段并将值放在第三个中,并在每个新动态表单中执行此操作(假设字段1是价格,字段2是金额和字段3总计).
使用我的代码,我能够做到这一点,但只能在第一个动态表单上.
<?PHP
$script = <<< JS
$('#itemsfactura-{$i}-cantidad').change(function(){
    var cantidad = $(this).val();
    var precio = $('#itemsfactura-{$i}-precio').val();
    $('#itemsfactura-{$i}-@R_862_10586@l_item').val(cantidad * precio);
});
JS;
$this->registerJs($script);
?>

这是动态表单字段的代码

...
<div class="row">
    <div class="col-sm-4">
        <?= $form->field($modelItemFactura,"[{$i}]precio")->texTinput(['maxlength' => true]) ?>
    </div>
    <div class="col-sm-4">
        <?= $form->field($modelItemFactura,"[{$i}]cantidad")->texTinput(['maxlength' => true]) ?>
    </div>
    <div class="col-sm-4">
        <?= $form->field($modelItemFactura,"[{$i}]@R_862_10586@l_item")->texTinput(['maxlength' => true]) ?>
    </div>
</div>...
形成
<div class="col-sm-4">
    <?= $form->field($modelItemFactura,"[{$i}]precio")->texTinput(['maxlength' => true,'onchange' => 'getProduct($(this))','onkeyup' => 'getProduct($(this))']) ?>
</div>
<div class="col-sm-4">
    <?= $form->field($modelItemFactura,"[{$i}]cantidad")->texTinput(['maxlength' => true,'onkeyup' => 'getProduct($(this))']) ?>
</div>

JS

function getProduct(item) {
    var index  = item.attr("id").replace(/[^0-9.]/g,"");
    var @R_862_10586@l = current = next = 0;

    var id = item.attr("id");
    var myString = id.split("-").pop();

    if(myString == "precio") {
        fetch = index.concat("-cantidad");
    } else {
        fetch = index.concat("-precio");
    }

    temp = $("#itemsfactura-"+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)) {
        @R_862_10586@l = parseInt(current) * parseInt(next);
    }

    @R_862_10586@lItem = "itemsfactura-".concat(indeX).concat("-@R_862_10586@l_item");

    $("#"+@R_862_10586@lItem+"").val(@R_862_10586@l);
}

大佬总结

以上是大佬教程为你收集整理的Yii2-dynamicforms和javascript全部内容,希望文章能够帮你解决Yii2-dynamicforms和javascript所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:javascript