如何使用javascript在文本文件中将数据写入一行

时间:2013-07-05 09:10:53

标签: javascript html text-files

我正在尝试使用JavaScript函数在.txt文件中写入数据。 我的功能: -

function WriteToFile()
{
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var s = fso.CreateTextFile("test.txt", true);
    for(var j=1;j<9;j++)
    {
        if(document.getElementById('chk'+j).checked)
        {
            s.WriteLine('  ');
            s.WriteLine(document.getElementById('item'+j).innerText);
            s.WriteLine(',');
            s.WriteLine(document.getElementById('txtBx'+j).value);
            s.WriteLine(',');
            s.WriteLine(document.getElementById('txtBxx'+j).value);
            s.WriteLine(';');
        }
    }
    alert("written");
    s.Close();
}

现在问题是数据写在新行上。 (也许是因为我正在使用s.WriteLine?) 我希望所有数据都写在一行中。我怎么能做到这一点?

详细说明,我目前的输出看起来像这样

abc
,
1
,
cdf
;

def
,
3
,
ehi
;

我想要这样 -

abc,1,cdf;def,3,ehi;

2 个答案:

答案 0 :(得分:2)

使用s.write()代替s.writeLine()。顾名思义,第二个函数用于编写整行,并在它之后添加换行符。

答案 1 :(得分:0)

只需将所有内容添加到变量中,然后编写该行。

var st = document.getElementById('item'+j).innerText;
st += ','
st += document.getElementById('txtBx'+j).value;
s.WriteLine(st);
相关问题