我在小提琴示例中遇到两个问题。首先,当您单击复选框时,它应该立即进行计算,但事实并非如此。您只需首先更改保管箱值,然后检查值,然后它就可以工作了。有谁知道从复选框中选择值后该怎么做才能得到结果?我需要默认选择标准选项。第二件事是我需要从相同的值但小于 10% 和 20% 的值中获得结果。
var checkboxes = document.querySelectorAll('.sum')
var select = document.querySelector('#select')
var total = document.querySelector('#payment-total,#payment-total-2')
var checkboxesTotal = 0
var selectTotal = 0
checkboxes.forEach(function(input) {
input.addEventListener('change', onCheckboxSelect)
})
select.addEventListener('change', onSelectChange)
function onCheckboxSelect(e) {
var sign = e.target.checked ? 1 : -1
checkboxesTotal += sign * parseInt(e.target.value, 10)
renderTotal()
}
function onSelectChange(e) {
var value = parseInt(e.target.value, 10)
if (!isNaN(value)) {
selectTotal = value
renderTotal()
}
}
function renderTotal() {
total.innerHTML = checkboxesTotal * selectTotal
}
<ul>
<li><label class="checkbox" for="Checkbox1">
<input value="190" type="checkbox" class="sum"> Value 1
</label></li>
<li><label class="checkbox">
<input value="190" type="checkbox" class="sum"> Value 2
</label></li>
<li><label class="checkbox">
<input value="170" type="checkbox" class="sum"> Value 3
</label></li>
</ul>
<br/>
<br/>
<select id="select" name="select">
<option value="1" class="sum">Basic</option>
<option value="2" class="sum" selected="selected">Standard</option>
<option value="3" class="sum">Premium</option>
</select>
<br/>
<p>3 months <br>sum: <span id="payment-total">0</span> ;- month.</p>
<p>6 months (10% off) <br>sum: <span id="payment-total-2">0</span> ;- month.</p>
<p>12 months (20% off) <br>sum: <span id="payment-total-3">0</span> ;- month.</p>
拉丁的传说
凤凰求蛊
相关分类