Javascript checkbox onChange

问题描述:

我在表单中有一个复选框,我希望它可以按照以下方案工作:

I have a checkbox in a form and I'd like it to work according to following scenario:

如果有人检查它, code> totalCost )应设置为 10 。然后,如果我返回并取消选中它,函数 calculate()根据其他参数设置 totalCost 的值在形式。

if someone checks it, the value of a textfield (totalCost) should be set to 10. Then, if I go back and uncheck it, a function calculate() sets the value of totalCost according to other parameters in the form.

基本上,我需要的部分,当我检查复选框我做一件事,当我取消选中它,我做另一个。

So basically, I need the part where, when I check the checkbox I do one thing and when I uncheck it, i do another.

function calc()
{
  if (document.getElementById('xxx').checked) 
  {
      document.getElementById('totalCost').value = 10;
  } else {
      calculate();
  }
}

HTML

<input type="checkbox" id="xxx" name="xxx" onclick="calc();"/>