I have a code where it saves multiple textarea values in a text file. However, it does not display the line breaks I indicated after saving it. It only identifies the line breaks w/c are manually put within the textarea. Below is the code. Please help.
我有一个代码,它在文本文件中保存多个textarea值。但是,它不会显示保存后我指示的换行符。它只识别w / c的换行符是手动放入textarea中的。以下是代码。请帮忙。
<script>
var TestVar = new Array();
var i = 0;
function save()
{
TestVar[i] = document.getElementById("text1").value + "\n" + document.getElementById("text2").value;
mydoc = document.open();
mydoc.write(TestVar);
mydoc.execCommand("saveAs",true,"TicketID.txt");
mydoc.close();
}
</script>
</head>
<body>
<form id=formtest>
<textarea name="textarea" id="text1"></textarea>
<textarea name="textarea" id="text2"></textarea>
<input type="button" value="save" onclick="save()">
</form>
</body>
2 个解决方案
#1
14
text = text.replace(/\n\r?/g, '<br />');
text is value from textarea.
text是来自textarea的值。
#2
11
The problem stems from the fact that line breaks (\n
) are not the same as HTML <br />
tags.
问题源于换行符(\ n)与HTML
标记不同的事实。
Try this:
var text = document.forms[0].txt.value;
text = text.replace(/\n\r?/g, '<br />');
Edit, try this as the js:
编辑,试试这个为js:
var text = document.forms[0].txt.value;
if (text === true) { text = text.replace(/\n\r?/g, '<br />'); }
var TestVar = new Array(i);
var i = 0;
function save()
{
TestVar[i] = document.getElementById("text1").value + "/n" + document.getElementById("text2").value;
mydoc = document.open();
mydoc.write(TestVar);
mydoc.execCommand("saveAs",true,"TicketID.txt");
mydoc.close();
}
#1
14
text = text.replace(/\n\r?/g, '<br />');
text is value from textarea.
text是来自textarea的值。
#2
11
The problem stems from the fact that line breaks (\n
) are not the same as HTML <br />
tags.
问题源于换行符(\ n)与HTML
标记不同的事实。
Try this:
var text = document.forms[0].txt.value;
text = text.replace(/\n\r?/g, '<br />');
Edit, try this as the js:
编辑,试试这个为js:
var text = document.forms[0].txt.value;
if (text === true) { text = text.replace(/\n\r?/g, '<br />'); }
var TestVar = new Array(i);
var i = 0;
function save()
{
TestVar[i] = document.getElementById("text1").value + "/n" + document.getElementById("text2").value;
mydoc = document.open();
mydoc.write(TestVar);
mydoc.execCommand("saveAs",true,"TicketID.txt");
mydoc.close();
}