所以,我正在制作一个基本的笔记应用程序,我有一个文本区域和一个提交按钮,供用户输入笔记。在按下按钮后,我很难将文本区域重置为空白。我试着在网上找到一个解决方案,但当我在前端只使用普通javascript时,我找到的都是使用jquery的解决方案。
这是html:
<textarea class="noteInput" rows="10" cols="46" placeholder="Click to start typing your note" maxlength="314"></textarea>
<button class="submitNoteButton">Add Note</button>
这是迄今为止的javascript,唯一有问题的部分是noteInput.value="quot;最后:
submitNoteButton.addEventListener('click', function submitNote() {
let noteInput = document.querySelector('.noteInput').value; //Get input from the textArea
noteList.push(noteInput); //Add the note to the array of notes to be displayed
console.log(noteList);
modalbg.style.display = 'none'; //Close the note after the submit button is
pressed
//If the user isn't logged in, no need to pull from the database
if (!loggedIn)
{
let notes = document.querySelector('.notes'); //Notes div in the html
if (notes.length != 0) //If there are already notes, clear them before doing the for loop
{
notes.innerHTML = '';
}
//For each note in the noteList, display it in the html
for (let i = 0; i < noteList.length; i++)
{
let note = document.createElement('note'); //Create an element for each note
note.innerHTML = noteList[i]; //Make the text of each note
note.className = 'note';
notes.appendChild(note); //Append each note to the notes div
}
noteInput.value = "";
}
})
除了这件小事,一切都在按我需要的方式进行。它告诉我";无法在字符串"上创建属性;然后是最后输入的字符串。不知道为什么noteInput.value被视为字符串,而不是我定义的文本区域。有什么帮助吗?
您将noteInput定义为textarea的值,而不是textarea元素本身。这应该可以修复
let noteInput = document.querySelector('.noteInput');
noteList.push(noteInput.value);
尝试更改noteInput.value = ""
到noteInput = ""