简体   繁体   English

语法错误:丢失; 在javascript循环中串联时的before语句

[英]SyntaxError: missing ; before statement while concate in javascript loop

I used contact in php like this 我在php中这样使用contact

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

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

When I wanted to change it into javascript: 当我想将其更改为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 />';
}

It showed me this error 它告诉我这个错误

Error: SyntaxError: missing ; 错误:SyntaxError:丢失; before statement 声明前
Line: 311, Column: 32 行:311,列:32
Source Code: var location_space += new_location[i] + ', '; 源代码:var location_space + = new_location [i] +',';

So what is wrong? 那怎么了?

You have already defined location_space and location_formation , so you only need to assign a value to them, not redeclare them inside the for() loop: 您已经定义了location_spacelocation_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 />';
}

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

You should not define the location_space and location_formation in the for again. 您不应再次在for中定义location_spacelocation_formation
Change your code to this: 将代码更改为此:

 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 />';
 }

Change it to : 将其更改为:

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

and try 并尝试

Try to replace with: 尝试替换为:

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 />';
}

You're trying to declare location_space and location_formation again and use it like it was declared before "+=". 您尝试再次声明location_space和location_formation,并像在“ + =”之前声明的那样使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM