重置文本区域问题,"Cannot create property on string"
Resetting textarea problem, "Cannot create property on string"
因此,我正在制作一个基本的笔记应用程序,并且我有一个文本区域和一个供用户输入笔记的提交按钮。尽管在按下按钮后,我无法将文本区域重置为空白。我试着在网上找到一个解决方案,但当我只在前端使用香草 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 = "":
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 定义为文本区域的值,而不是文本区域元素本身。这应该可以解决问题
let noteInput = document.querySelector('.noteInput');
noteList.push(noteInput.value);
尝试更改 noteInput.value = ""
到
noteInput = ""
因此,我正在制作一个基本的笔记应用程序,并且我有一个文本区域和一个供用户输入笔记的提交按钮。尽管在按下按钮后,我无法将文本区域重置为空白。我试着在网上找到一个解决方案,但当我只在前端使用香草 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 = "":
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 定义为文本区域的值,而不是文本区域元素本身。这应该可以解决问题
let noteInput = document.querySelector('.noteInput');
noteList.push(noteInput.value);
尝试更改 noteInput.value = ""
到
noteInput = ""