Javascript 用包含多行的值填充输入
Javascript fill input with value containing multiple lines
我的表单中有一个输入元素。当我按下提交按钮时,我调用了一个函数,该函数将 Ace Editor Api
中的 C++ 程序写入其中
我的 Ace 编辑器 getValue() 函数 returns 我通过
写入输入元素的代码
document.getElementById("id_code").value = editor.getValue();
//alert(editor.getValue());
//alert(document.getElementById("id_code").value);
//id_code is id of my input element in html
//this snippet is called from function onclick submit button
但 ace 编辑器包含被此代码段转义的新行。就像我在浏览器中提醒了两件事(作为评论提到),这里是截图
和
如您所见,这两个换行符不同,我不希望这样。如何解决?
您可以像下面这样在客户端添加一个功能
<script type="text/javascript" language="javascript">
function repCRLFData(value)
{
var newValue = null;
var cr = "\r";
var lf = "\n";
try
{
newValue = value.split(cr).join("");
newValue = newValue.split(lf).join("");
}
catch(e)
{
alert("repCRLFData Error: " + e.Message);
}
finally
{
}
return newValue;
}
</script>
并使用
调用它
document.getElementById("id_code").value = repCRLFData(editor.getValue());
html 中的输入元素仅包含 1 行。所以要存储多行文本,使用textarea!
我的表单中有一个输入元素。当我按下提交按钮时,我调用了一个函数,该函数将 Ace Editor Api
中的 C++ 程序写入其中我的 Ace 编辑器 getValue() 函数 returns 我通过
写入输入元素的代码document.getElementById("id_code").value = editor.getValue();
//alert(editor.getValue());
//alert(document.getElementById("id_code").value);
//id_code is id of my input element in html
//this snippet is called from function onclick submit button
但 ace 编辑器包含被此代码段转义的新行。就像我在浏览器中提醒了两件事(作为评论提到),这里是截图
和
如您所见,这两个换行符不同,我不希望这样。如何解决?
您可以像下面这样在客户端添加一个功能
<script type="text/javascript" language="javascript">
function repCRLFData(value)
{
var newValue = null;
var cr = "\r";
var lf = "\n";
try
{
newValue = value.split(cr).join("");
newValue = newValue.split(lf).join("");
}
catch(e)
{
alert("repCRLFData Error: " + e.Message);
}
finally
{
}
return newValue;
}
</script>
并使用
调用它document.getElementById("id_code").value = repCRLFData(editor.getValue());
html 中的输入元素仅包含 1 行。所以要存储多行文本,使用textarea!