請幫忙。我在一個 div 中有不同名稱的總和值,需要執行“如果所有值的總和(0 1 6 或 1 1 2 等)>= 3 do * 3000,否則執行 * 4000(而不是 * 3000) " 我嘗試添加此代碼,但無法正常作業。無法理解為什么。我不專業對不起這里是https://codepen.io/evg33/pen/OJvVJMe我的代碼
jQuery(function ($) {
$('input[type=radio]').each(function(){
$(this).change(calculate);
});
function calculate() {
var result = 0;
$('input[type=radio]:checked').each(function(){
const val = Number($(this).val());
result = val * (val >= 3 ? 3000 : 4000);
});
$("#result").html(result);
}
});
<div><label >
<input type="radio"
name="one" value="0">
</label>
<label >
<input type="radio"
name="one" value="1">
</label>
<label >
<input type="radio"
name="two" value="1">
</label>
<label >
<input type="radio"
name="two" value="6">
</label>
<label >
<input type="radio"
name="three" value="2">
</label>
<label >
<input type="radio"
name="three" value="7">
</label>
</div>
<span id="result">- </span>
這段代碼運行良好(所以我猜問題不在 html 中)。所有總和都是正確的
jQuery(function ($) {
$('input[type=checkbox]').each(function(){
$(this).change(calculate);
});
function calculate() {
var result = 0;
$('input[type=checkbox]:checked').each(function(){
result = Number($(this).val()) * 3000;
});
$("#result").html(result);
}
});
uj5u.com熱心網友回復:
要將乘法應用于總值,您需要首先計算所選無線電輸入的總值。然后你可以計算出要應用的因素。
jQuery($ => {
let calcTotal = () => {
let total = $radio.filter(':checked').get().reduce((t, el) => t el.value, 0);
let factor = total >= 3 ? 3000 : 4000;
return total * factor;
}
let $total = $('#result');
let $radio = $(':radio').on('change', () => {
$total.text(calcTotal());
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div>
<label class="radio">
<input type="radio" name="one" value="0"> 0
</label>
<label class="radio">
<input type="radio" name="one" value="1"> 1
</label><br />
<label class="radio">
<input type="radio" name="two" value="1"> 1
</label>
<label class="radio">
<input type="radio" name="two" value="6"> 6
</label><br />
<label class="radio">
<input type="radio" name="three" value="2"> 2
</label>
<label class="radio">
<input type="radio" name="three" value="7"> 7
</label>
</div>
<span id="result">- </span>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/497884.html
標籤:jQuery
上一篇:從表中洗掉行時正在洗掉表頭
下一篇:如何將多個按鈕選擇添加到Div
