1

我做了一个计算函数(游戏统计计算器),它工作正常,但是当我取消选中该值时,它不会减去它通过选中框添加的值,我知道代码看起来有点难看,但我是初学者在 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 应该是我刚刚在那里进行初始化的一项,因为我在这个例子中需要它。

4

2 回答 2

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;
  • 仅当检查变量为真时才初始化其他本地变量,这会导致您报告的 NaN 值;
  • 如果你必须检查一个条件和他的对立面,最好是上面的 if(condition)/else表格if(condition)/if(!condition)
  • 要挑剔:你不会在你的函数中返回任何值,只是设置一些 dom 元素的值
  • 您声明spSum局部变量,将其设置为 0 并且不使用它。
  • 也已hpSum声明但未使用。
于 2013-04-12T17:25:02.343 回答
0

因为您仅在checkis时才分配变量的值true。您必须将它们放在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;
};
于 2013-04-12T17:55:51.927 回答