0

我像这样在php中使用了联系人

$location_space     = '';
$location_formation = '';

foreach($location as $formation) {
    $location_formation .= $formation.', ';
    $location_space     .= $formation.'<br />';
}

当我想将其更改为 javascript 时:

var location_space      = '';
var location_formation  = '';

for (i = 0; i < new_location.length; i++) {
    var location_space      += new_location[i] + ', ';
    var location_formation  += new_location[i] + '<br />';
}

它向我显示了这个错误

错误:语法错误:缺失;before 语句
Line: 311, Column: 32
Source Code: var location_space += new_location[i] + ', ';

那么有什么问题呢?

4

5 回答 5

1

您已经定义了location_spaceand location_formation,因此您只需为它们分配一个值,而不是在for()循环内重新声明它们:

var location_space      = '';
var location_formation  = '';

for (var i = 0; i < new_location.length; i++) 
{
    location_space      += new_location[i] + ', ';
    location_formation  += new_location[i] + '<br />';
}
于 2013-01-15T11:26:04.253 回答
0

尝试替换为:

var location_space      = '';
var location_formation  = '';

for(i=0; i<new_location.length; i++) {
    location_space      += new_location[i] + ', ';
    location_formation  += new_location[i] + '<br />';
}

您正在尝试再次声明 location_space 和 location_formation 并像在“+=”之前声明的那样使用它。

于 2013-01-15T11:28:15.177 回答
0

您已经在 for 循环之前声明了变量,因此如果您var从循环内部删除 s,则字符串连接应该可以工作。

于 2013-01-15T11:25:04.643 回答
0

您不应该再次在 for 中定义location_spaceand location_formation
将您的代码更改为:

 var location_space      = '';
 var location_formation  = '';
 for(var i = 0; i < new_location.length; i++)
 {
     location_space      += new_location[i] + ', ';
     location_formation  += new_location[i] + '<br />';
 }
于 2013-01-15T11:25:07.017 回答
0

将其更改为:

for (i = 0; i < new_location.length; i++) {
    location_space      += new_location[i] + ', ';
    location_formation  += new_location[i] + '<br />';
}

并尝试

于 2013-01-15T11:26:36.263 回答