Jquery 增加函数内全局变量的值。each()
我有以下代码:Jquery 增加函数内全局变量的值。each(),jquery,Jquery,我有以下代码: $(document).ready(function() { var MidUpperArmCircumference = 0; var TricepsSkinfold = 0; function checkMethod(method,parameters){ $('#'+method+'_check').change(function() { if (this.checked == true) { $.each(parameters, f
$(document).ready(function() {
var MidUpperArmCircumference = 0;
var TricepsSkinfold = 0;
function checkMethod(method,parameters){
$('#'+method+'_check').change(function() {
if (this.checked == true) {
$.each(parameters, function() {
$('.'+this).css('color','blue');
this++; //HERE IS THE ERROR!
});
}
});
}
var parametersMuscleArea = ['TricepsSkinfold', 'MidUpperArmCircumference'];
checkMethod('MidUpperArmMuscleArea',parametersMuscleArea);
});
如何增加
$内的变量Tricepskinfold
和MidUpperArmPercentage
。每个函数?这是字符串,而不是局部变量的引用。不能通过增加字符串来增加变量。此外,您还试图通过字符串中给定的名称引用局部变量。对于局部变量,这只能通过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.
});
}
});
}
var ns={
中上臂围:0,
三头肌皮褶:0
};
函数检查方法(方法、参数){
$(“#”+方法+“#检查”).change(函数(){
if(this.checked==true){
$.each(参数,函数(){
$('.+this.css('color','blue');
ns[这个]++;//很简单,只要++
它们就可以了:
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.
});
}
});
}
我想这对我来说更像一个问题……当变量在$(document.ready()中声明时,这仍然有效吗
block?@ManseUK。是的,你可以试试。其余的代码都是正确的。只有被标记的行才是问题所在。gdoron查看最后两行。该函数将使用不同的参数调用多次。但是thx。“这是一个对象引用”不总是。在严格模式下,如果一个设置为thisArg,它将是一个原语,这里就是这种情况。“你不能“增加”一个对象。”除非它是一个数字对象,或者任何值可以转换为数字的对象(在这种情况下,它将被强制为一个原语)。@amnotiam呃,那确实是垃圾。我已经更改了那部分(关于该问题)。