如何使用javascript在文本文件的单行中写入数据
我正在尝试使用JavaScript函数在.txt文件中写入数据。 我的功能:-如何使用javascript在文本文件的单行中写入数据,javascript,html,text-files,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('ch
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();
}
我想要这样-
abc,1,cdf;def,3,ehi;
只需将所有内容添加到变量中,然后写入行
var st = document.getElementById('item'+j).innerText;
st += ','
st += document.getElementById('txtBx'+j).value;
s.WriteLine(st);
使用
s.write()
代替s.writeLine()
。顾名思义,第二个函数用于写整行,并在其后添加一个换行符。试试d.write,看看是否有效,谢谢。我怀疑s.writeline是罪魁祸首,但不知道还有其他选择。
var st = document.getElementById('item'+j).innerText;
st += ','
st += document.getElementById('txtBx'+j).value;
s.WriteLine(st);