function getNote() {
var textField = document.getElementById('textField').value;
var result = document.getElementById('result');
result.innerHTML = textField;
}
var submitButton = document.getElementById('submitButton');
submitButton.addEventListener('click',getNote);
<html lang="en">
<head>
<div id="wrapper">
<div class="topnav" id="myTopnav">
<a href="#home">NoteTaker</a>
</div>
<title>Note Taker</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></head>
<body id="style">
<div id ="centerField">
<h1> New Note</h1>
<textarea id="textField"rows="8" cols="70%" placeholder ="Type Here."></textarea>
<br>
<button type="button" id="submitButton" class="btn btn-warning">Submit Information</button>
</div>
<div id ="leftField" >
<h2> Saved Notes</h2>
<p id='result'></p></div>
<div id="container"></div>
</div>
</div>
</body>
</html>
我希望每个值都不断添加到该字段中。因此,当我提交测试然后提交帮助时,该字段应该同时包含两者。现在它添加然后替换,现在我被难住了。
最佳答案
如果您不想对其进行验证并保留追加,请获取旧值并向其中添加新值。
result.innerHTML = result.innerHTML + " " + textField;
function getNote() {
var textField = document.getElementById('textField').value;
var result = document.getElementById('result');
result.innerHTML = result.innerHTML + " " + textField;
}
var submitButton = document.getElementById('submitButton');
submitButton.addEventListener('click',getNote);
<html lang="en">
<head>
<div id="wrapper">
<div class="topnav" id="myTopnav">
<a href="#home">NoteTaker</a>
</div>
<title>Note Taker</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"></head>
<body id="style">
<div id ="centerField">
<h1> New Note</h1>
<textarea id="textField"rows="8" cols="70%" placeholder ="Type Here."></textarea>
<br>
<button type="button" id="submitButton" class="btn btn-warning">Submit Information</button>
</div>
<div id ="leftField" >
<h2> Saved Notes</h2>
<p id='result'></p></div>
<div id="container"></div>
</div>
</div>
</body>
</html>
关于javascript - 我将如何附加结果值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45366007/