gpt4 book ai didi

jquery - 如何使用 keyup 计算 onchange

转载 作者:行者123 更新时间:2023-12-01 02:18:57 24 4
gpt4 key购买 nike

我有 3 个输入。套餐 - 总计 - 价格

如何让套餐改变时价格也跟着改变。

$('#the_total').keyup(function () {
var the_total = $("#the_total").val();
var get_package = $("#package").val();
if (get_package == 'Package A') {
var total = the_total * 100;
} else {
var total = the_total * 200;
}
$("#price").val(total);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="package">
<option value="Package A">Package A</option>
<option value="Package B">Package B</option>
</select>
<input type="text" id="the_total" name="total">
<input type="text" id="price" name="price">

最佳答案

在更改事件时触发 keyup

$('#the_total').keyup(function() {
var the_total = $("#the_total").val();
var get_package = $("#package").val();
if (get_package == 'Package A') {
var total = the_total * 100;
} else {
var total = the_total * 200;
}
$("#price").val(total);
});

$('#package').change(function() { // on change
$('#the_total').keyup(); // trigger this
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="package">
<option value="Package A">Package A</option>
<option value="Package B">Package B</option>
</select>

<input type="text" id="the_total" name="total">
<input type="text" id="price" name="price">

关于jquery - 如何使用 keyup 计算 onchange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33756775/

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