Yii2:让表单文件的值取决于另一个,如何编写ajax

时间:2015-03-27 14:39:36

标签: php ajax yii2

如下所示,可以输入amountfee-rate字段,但fee会自动设置(fee=rate*amount),因此人们不会计算它他们自己。

<div class="loanr" id="loan-rate"><?= $form->field($model, 'amount')?></div>
<div class="loanr" id="loan-rate"><?= $form->field($model, 'fee-rate')?></div>

如何更改此行:

<div class="loanr" id="loan-fee"><?= $form->field($model, 'fee')?></div>

我可以使用ajax,但我不知道如何以Yii形式填写此内容。

1 个答案:

答案 0 :(得分:1)

在视图中输入此代码:

<?php
$this->registerJs("$('#rate, #amount').keyup(function(){
        var rate = $('#rate').val(),
            amount = $('#amount').val(),
            fee = 0;
        rate = rate - 0;//convert to integer
        amount = amount - 0;//convert to integer
        fee = rate * amount;
        $('#fee').val(fee);
});"); 
?>

并形成:

<div class="loanr" id="loan-rate"><?= $form->field($model, 'amount')->textInput(['id' => 'amount'])?></div>
<div class="loanr" id="loan-rate"><?= $form->field($model, 'fee-rate')->textInput(['id' => 'rate'])?></div>
<div class="loanr" id="loan-fee"><?= $form->field($model, 'fee')->textInput(['id' => 'fee'])?></div>