如何修复函数内的“else if”块?

huangapple go评论61阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年2月8日 14:42:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定