gpt4 book ai didi

javascript - 使用JS将值自动计算到表单字段中

转载 作者:行者123 更新时间:2023-12-03 07:33:34 25 4
gpt4 key购买 nike

我有六个字段可以在我的 Yii2 表单小部件中编辑。我想要做的是,一旦我将整数值输入到可编辑字段中,就会根据给定的输入自动计算“绿叶净重”。我的编码工作完美。但问题是,如果我更改字段中已输入的值,它不会更新“绿叶净重”只读字段。

$(document).ready(function(){
var A = $('#A');
var B = $('#B');
var C = $('#C');
var D = $('#D');
var E = $('#E');

E.change(function(e){
var result = ((A.val() - B.val()) * C.val())/100;
var result2 = ((A.val() - B.val()) * D.val()) / 100;
var final = ((A.val() - B.val()) - result - result2) - E.val();
Math.round(final);
$('#F').val(final);
})

});

我的表单小部件编码(更新的代码)

 <?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'Gross_weight',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',

]
])->textInput(['id' => 'A', **'class' => 'factors'** ]) ?>

<div class="panel panel-primary" style="padding: 10px;">

<div style="text-align: center; color: #008000;"><b>Deductions</b></div>

<?= $form->field($model, 'Bags_count',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput() ?>

<?= $form->field($model, 'Bags_weight',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',

]
])->textInput(['id' => 'B', **'class' => 'factors'** ]) ?>

<?= $form->field($model, 'Course_leaf',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',

]
])->textInput(['id' => 'C', **'class' => 'factors'**]) ?>

<?= $form->field($model, 'Water',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',

]
])->textInput(['id' => 'D', **'class' => 'factors'**]) ?>

<?= $form->field($model, 'Boiled_leaf',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',

]
])->textInput(['id' => 'E',**'class' => 'factors'**]) ?>
</div>
<?= $form->field($model, 'Greanleaf_net_weight',[
'feedbackIcon' => [
'default' => 'shopping-cart',
'success' => 'ok',
'error' => 'exclamation-sign',
]
])->textInput(['disabled' => 'disabled', 'id' => 'F']) ?>

form wiget

谢谢大家。

最佳答案

为了对所有输入字段的更改执行计算,您必须向所有输入字段添加一个类,并使用该类作为更改事件的 jQuery 选择器。

 <input id="A" class="factors" ... />
...
...

$(".factors").change(function(e) {
...
...
}

See Demo

关于javascript - 使用JS将值自动计算到表单字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35715350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com