在形式上,我想在rate的值(另一个文本输入)时插入amount的值(文本输入)。将插入数量的值是比率和数量的乘积。数量值取自数据库。在这里,rate的值将由用户插入。插入金额后,该值无法更改。我怎样才能做到这一点?
我试过下面的代码,但结果每次都显示为零:
看法
<div class="form-group">
<label for="exampleInputPassword1">Rate</label>
<input type="text" class="form-control" name="rate" placeholder="Rate" required>
</div>
<div class="form-group">
<label>Quantity</label>
<input type="text" class="form-control" name="quantity" placeholder="quantity" value="<?php echo $object['sheet']; ?> " required readonly>
</div>
<div class="form-group">
<label for="exampleInputPassword2">Amount</label>
<?php
$sheet = $object['sheet'];
$rate = $this->input->post('rate');
$amount = $sheet*$rate;
echo $amount;
}
?>
<input type="text" class="form-control" name="amount" value="<?php echo $amount;?>" placeholder="Amount" required>
</div>
您可以使用jQuery来做到这一点:
$(document).ready(function() {
$('input[name="rate"]').on('change',function() {
var amount = $(this).val()*$('input[name="quantity"]').val();
$('input[name="amount"]').val(amount);
})
});
不要忘了包括jQuery api。另外,不要忘记在提交表单后验证金额,因为用户可以轻松地操纵输入的值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句