2012-03-06 13 views

答えて

2

thisは文字列であり、ローカル変数への参照ではありません。文字列を増やして変数を増やすことはできません。また、変数ローカルを文字列で指定された名前で参照しようとしています。ローカル変数では、これはevalを介してのみ、または変数に名前を付けることによって行うことができます。

これは動作します:

var ns = { 
    MidUpperArmCircumference: 0, 
    TricepsSkinfold: 0 
}; 

function checkMethod(method,parameters){ 
    $('#'+method+'_check').change(function() { 
     if (this.checked == true) { 
      $.each(parameters, function() { 
       $('.'+this).css('color','blue'); 
       ns[this]++; // <-- Fixed. 
      }); 
     } 
    }); 
} 
+0

* "' this'がオブジェクト参照である" *とは限りません。 strictモードでは、* thisArg *として設定されている場合はプリミティブになります。 * "オブジェクトを増やすことはできません" * Numberオブジェクトでないか、値をtoNumber *に変換できるオブジェクトでない限り(プリミティブに強制されます)。 –

+1

@amnotiamええ、それは確かにごみでした。私はその部分を(質問に関連して)変更しました。 –

+0

が働いた! thxそんなに! – Munir

0

簡単に、ちょうど++それら

function checkMethod(method,parameters){ 
    $('#'+method+'_check').change(function() { 
    if (this.checked == true) { 
     $.each(parameters, function() { 
      TricepsSkinfold++; 
      MidUpperArmCircumference++; 
      ... 
      ... 
      $('.'+this).css('color','blue'); // That looks wrong as well... 
      // as "this" is the current item of the iteration. 
     }); 
    } 
    }); 
} 
+0

これは私にとってはもっと疑問です。変数が '$(document).ready()'ブロックの中で宣言されてもこれは動作しますか? – ManseUK

+0

@ManseUK。はい、それを試すことができます。 – gdoron

+0

私はそう思います:-) – ManseUK