-1

我有这样的代码:

var exist_file = document.getElementById('exist_file').value;
        document.getElementById('dialog-confirm-upload').innerHTML=

             "The file" + 
             for (var i=0;i<exist_file.length;i++){
                 exist_file + ","
             }

             +
             "has been already transferred? Do you want to overwrite?"

exists_file 是一个包含一个或多个元素的数组:['hello', 'ok']。我想将值传递给innerHTML这样的。 "The file hello, ok has been already transferred? Do you want to overwrite?"我尝试了以下上面的代码,但事实证明这不是有效的语法。我怎样才能做到这一点?

4

2 回答 2

0
var exist_file = document.getElementById('exist_file').value,
     msg = "The file" + exist_file.toString() + "has been already transferred? Do you want to overwrite?";
    document.getElementById('dialog-confirm-upload').innerHTML= msg;

exist_file此代码段适用于数组或字符串的两种情况。

于 2013-07-04T06:13:58.980 回答
0

首先你必须将字符串解析成一个数组,然后你可以用逗号格式化它:

var exist_file = document.getElementById('exist_file').value;
document.getElementById('dialog-confirm-upload').innerHTML=
  "The file " + 
  exist_file.substr(3, exist_file.length - 5).split("', u'").join(", ") +
  " has already been transferred. Do you want to overwrite?"

注意:这是最基本的解析形式。它适用于包含常规字符的字符串,但转义字符不是未转义的。

于 2013-07-04T06:25:36.247 回答