Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery上的奇怪问题。每个函数_Javascript_Jquery_Html - Fatal编程技术网

Javascript Jquery上的奇怪问题。每个函数

Javascript Jquery上的奇怪问题。每个函数,javascript,jquery,html,Javascript,Jquery,Html,我的密码有个奇怪的问题 我有点像 var test, result; $(".class td").each(function(){ test = $(this).find('input').attr('size'); if(test){ result = test * 10; } console.log(result); }) 并非每个td都有输入字段,因此test可能是未定义的。但是,在定义测试后,控制台将始终从测试输出一个值 例如

我的密码有个奇怪的问题

我有点像

var test, result;
$(".class td").each(function(){
     test = $(this).find('input').attr('size');
     if(test){
         result = test * 10;
     }
     console.log(result);
})
并非每个
td
都有输入字段,因此
test
可能是
未定义的。但是,在定义测试后,控制台将始终从
测试输出一个值

例如:

   undefined (test = undefined)
   undefined (test = undefined)
   undefined (test = undefined)
   200 (test = 20)
   200 (test = undefined)
   200 (test = undefined)
   200 (test = undefined)

我不知道这里发生了什么事。有人能帮我吗?谢谢。

在内部范围中定义
结果
,而不是在外部范围中定义,因为第一个结果有值,您的后续迭代仍然保留旧值,这就是您看到的

$(".class td").each(function(){
     var result , 
         test = $(this).find('input').attr('size');
     if(test){ //remove this check and you will see NaN for the iterations where test = undefined in your code
         result = test * 10;
     }
     console.log(result);
});
您还可以避免循环没有输入字段的td

$(".class td:has(input)").each(...


在内部范围中定义
result
,而不是将其保留在外部范围中,因为第一个结果具有值,您的后续迭代仍然保留旧值,这就是您所看到的

$(".class td").each(function(){
     var result , 
         test = $(this).find('input').attr('size');
     if(test){ //remove this check and you will see NaN for the iterations where test = undefined in your code
         result = test * 10;
     }
     console.log(result);
});
您还可以避免循环没有输入字段的td

$(".class td:has(input)").each(...


您只需获取选择器中的输入,并将每个项目的结果映射到一个数组:

var result = $('.class td input').map(function() {
  return $(this).attr('size') * 10;
}).toArray();

您只需获取选择器中的输入,并将每个项目的结果映射到一个数组:

var result = $('.class td input').map(function() {
  return $(this).attr('size') * 10;
}).toArray();

如果第四个td有一个输入,那么这看起来就像我期望的输出。。。您希望发生什么情况呢?不,它将始终从
结果中输出一个值,因为这就是您正在进行的控制台日志记录?如果只有第四个td有一个输入,这看起来就像我期望的输出。。。您希望发生什么情况呢?不,它将始终从
结果中输出一个值,因为这就是您要记录的内容?