如果未选中复选框,如何减去值

时间:2013-04-12 17:19:49

标签: javascript

我为计算(游戏统计计算器)创建了一个函数并且它工作正常但是当我取消选中该值时它不会通过选中框来减去它添加的值,我知道代码看起来有点难看但是我我是JS的初学者:

function MC () {
check=document.getElementById('check').checked;


if(check){

    var hp=Number(document.form.hp.value),
        sp=Number(document.form.sp.value),
        ea=Number(document.form.ea.value),
        ed=Number(document.form.ed.value),
        pa=Number(document.form.pa.value),
        pd=Number(document.form.pd.value);

    var hpSum = 0,spSum=0,eaSum=0,edSum=0,pdSum=0,paSum=0;

    eaSum = ea + 11;
    edSum = ed + 17;
    pdSum = pd + 17;
    paSum = pa + 11;


    document.form.ea.value=eaSum;
    document.form.ed.value=edSum;
    document.form.pa.value=paSum;
    document.form.pd.value=pdSum;
} 

if(check == false){
    eaSum = ea - 11;
    edSum = ed - 17;
    pdSum = pd - 17;
    paSum = pa - 11;

    document.form.ea.value=eaSum;
    document.form.ed.value=edSum;
    document.form.pa.value=paSum;
    document.form.pd.value=pdSum;

    }
};

当我取消选中NaN时会返回(但它应该是一个数字)。也请不要MC应该是一个项目,我刚刚进行了初始化,因为我需要这个例子。

2 个答案:

答案 0 :(得分:2)

我会尝试这样的事情:

function MC () {
    var check=document.getElementById('check').checked;

    var hp=Number(document.form.hp.value),
        sp=Number(document.form.sp.value),
        ea=Number(document.form.ea.value),
        ed=Number(document.form.ed.value),
        pa=Number(document.form.pa.value),
        pd=Number(document.form.pd.value);

    var hpSum = 0,spSum=0,eaSum=0,edSum=0,pdSum=0,paSum=0;

    if(check){
        eaSum = ea + 11;
        edSum = ed + 17;
        pdSum = pd + 17;
        paSum = pa + 11;
    } else {
        eaSum = ea - 11;
        edSum = ed - 17;
        pdSum = pd - 17;
        paSum = pa - 11;
    }

    document.form.ea.value=eaSum;
    document.form.ed.value=edSum;
    document.form.pa.value=paSum;
    document.form.pd.value=pdSum;
} 

代码中的一般问题:

  • 检查局部变量错过了var;
  • 仅当check变量为true时才初始化其他本地变量 这会导致您报告的NaN值;
  • 如果你必须检查一个条件而他的对面是最好的 if(condition)/else上的if(condition)/if(!condition)表单;
  • 挑剔:你没有在函数中返回任何值,只需设置一些dom值 元素
  • 您声明spSum局部变量,将其设置为0并且不使用它。
  • hpSum也已声明但未使用。

答案 1 :(得分:0)

因为仅当checktrue时才指定变量的值。你必须把它们放在if声明中:

function MC () {
   // user `var`! Otherwise you're polluting the global scope
   var checked = document.getElementById('check').checked;

   // if it's checked then the `sign` is +, otherwise -.
   var sign = ~-(checked << 1);

   // don't repeat yourself, and cache `form` for a faster access.
   // I assume from your code that `form` is the name of your form, so:
   var form = document.forms["form"]; // reduce ambiguity

   // Unary plus operator: 
   // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Arithmetic_Operators
   var ea = +form.ea.value;
   var ed = +form.ed.value;
   var pa = +form.pa.value;
   var pd = +form.pd.value;
   // I removed `hp` and `sp` because not used

   // the multiplication operator takes the precedence
   var eaSum = ea + 11 * sign;
   var edSum = ed + 17 * sign;
   var pdSum = pd + 17 * sign;
   var paSum = pa + 11 * sign;

   // set the values back
   form.ea.value = eaSum;
   form.ed.value = edSum;
   form.pa.value = paSum;
   form.pd.value = pdSum;
};