通过 getElementById().value 在 textarea 中添加新行不工作
Add New Line in textarea by getElementById().value not Working
我是 Javascript 的新手。对不起,如果我说的有些荒谬。 None 我在此处找到的先前答案对我的情况有效...
该代码从数组循环生成的下拉列表中的选定选项中获取索引,并使用该索引对文本区域中的产品进行 post 描述。理想的是每行一个。但是每当我添加'\n'(仅为代码末尾的可视化而添加)或'
下拉列表本身消失了。尝试“
”也不起作用。
pr[] 是一个嵌套数组,第一个位置包含 10 种产品(前阿迪达斯足球)的描述,第二个位置包含价格。
函数 buy() 由按钮的 onclick 事件调用,每次调用它都会将一个产品添加到文本区域。
提前致谢!
textd=" ";
valord=0;
function buy() {
var e = document.getElementsByTagName("select");
var f = e[0].selectedIndex;
textd +=pr[f][0];
valore = valord += pr[f][1];
document.getElementById("compras").value=textd\n;
document.getElementById("valor").value ="R$ "+ valore+",00";
}
在文本区域添加文本时,需要在字符串末尾添加“\n”,这样“\n”就可以确保该行显示在新的一行而不是同一行。
看下面代码
<!DOCTYPE html>
<html>
<body>
<script>
function appendText()
{
debugger;
var ele = document.getElementById("textArea");
var text = ele.value;
text += "im clicked\n";
text +="clicked again\n";
text +="clicked third time\n";
text +="clicked forth time";
ele.value = text;
}
</script>
<textarea rows="4" cols="50" id="textArea">
At w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
<button type="button" onclick="appendText()">Click me </button>
</body>
</html>
您可能需要将代码更改为
textd +=pr[f][0] + "\n";
document.getElementById("compras").value=textd;
我是 Javascript 的新手。对不起,如果我说的有些荒谬。 None 我在此处找到的先前答案对我的情况有效...
该代码从数组循环生成的下拉列表中的选定选项中获取索引,并使用该索引对文本区域中的产品进行 post 描述。理想的是每行一个。但是每当我添加'\n'(仅为代码末尾的可视化而添加)或'
下拉列表本身消失了。尝试“
”也不起作用。
pr[] 是一个嵌套数组,第一个位置包含 10 种产品(前阿迪达斯足球)的描述,第二个位置包含价格。
函数 buy() 由按钮的 onclick 事件调用,每次调用它都会将一个产品添加到文本区域。 提前致谢!
textd=" ";
valord=0;
function buy() {
var e = document.getElementsByTagName("select");
var f = e[0].selectedIndex;
textd +=pr[f][0];
valore = valord += pr[f][1];
document.getElementById("compras").value=textd\n;
document.getElementById("valor").value ="R$ "+ valore+",00";
}
在文本区域添加文本时,需要在字符串末尾添加“\n”,这样“\n”就可以确保该行显示在新的一行而不是同一行。
看下面代码
<!DOCTYPE html>
<html>
<body>
<script>
function appendText()
{
debugger;
var ele = document.getElementById("textArea");
var text = ele.value;
text += "im clicked\n";
text +="clicked again\n";
text +="clicked third time\n";
text +="clicked forth time";
ele.value = text;
}
</script>
<textarea rows="4" cols="50" id="textArea">
At w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.
</textarea>
<button type="button" onclick="appendText()">Click me </button>
</body>
</html>
您可能需要将代码更改为 textd +=pr[f][0] + "\n"; document.getElementById("compras").value=textd;