英文:
How to fix "else if" block inside function?
问题
根据检查的输入,我的函数支付应该使用 cost.innerHTML
显示工资。
但我有问题。我的 "else if" 块没有起作用。
调试器没有帮助。给我一些建议如何解决这个问题。
英文:
Depends on checked input my function payment should display a salary with using cost.innerHTML
.
But I have problem. My block "else if" is not working.
<div class="form__propertyInputs">
<span>qwerty</span> <input type="radio" name="propInput" id="property" checked>
<span>Nonqwerty</span> <input type="radio" name="propInput" id="noneProperty">
</div>
<div class="form__ownershipInputs hide">
<span>physical</span><input type="radio" name="ownerShipInput" id="physical">
<span>judical</span><input type="radio" name="ownerShipInput" id="judical">
</div>
<div class="form__debt" id="debtBlock">
<span>Debt:</span>
<input type="text" id="debt">
</div>
<div class="form__result">
<span>Sum:</span>
<div class="form__result-input"></div>
</div>
const debtBlock = document.querySelector('#debtBlock')
const debt = document.querySelector('#debt')
const ownershipInputs = document.querySelector('.form__ownershipInputs')
const property = document.querySelector('#property')
const noneProperty = document.querySelector('#noneProperty')
const cost = document.querySelector('.form__result-input')
const physical = document.querySelector('#physical')
const judical = document.querySelector('#judical')
let minSalary = 6700
noneProperty.addEventListener('change', toggleData)
property.addEventListener('change', toggleData)
debt.addEventListener('input', payment)
function toggleData() {
ownershipInputs.classList.toggle('hide')
debtBlock.classList.toggle('hide')
cost.innerHTML = ""
debt.value = ""
}
function payment() {
if (property.checked) {
if ((debt.value * 0.02) <= (minSalary * 10)) {
cost.innerHTML = (debt.value * 0.02).toFixed(2) + " грн"
} else if ((debt.value * 0.02) > (minSalary * 10)) {
cost.innerHTML = (minSalary * 10) + " грн"
}
} else if (noneProperty.checked) {
if (physical.checked) {
cost.innerHTML = minSalary + " грн"
} else if (judical.checked) {
cost.innerHTML = minSalary * 2 + " грн"
}
}
}
Debugger didn`t help. Give me advise how to manage this issue.
答案1
得分: 0
为了使else if
块工作,添加以下代码:
judical.addEventListener('change', payment);
英文:
Next time provide all HTML markup for the question.
What I can se in this code why your else if
block in your code is not working because it is missing a debt.addEventListener('input', payment)
for the judical
radio button. The payment function should be called whenever the value of the debt input or the selection of the judical
radio button changes.
Add this code to make the else if
block work:
judical.addEventListener('change', payment)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论