简体   繁体   English

使用 Javascript 或 jQuery 从文本框输入创建数组,并可以在单击时删除数组元素

[英]Use Javascript or jQuery to create array from textbox input and with the possibility of removal of array elements onClick

If the user enters more words at once, convert them in array elements and show below the textbox with the possibility to remove (on click).如果用户一次输入更多单词,请将它们转换为数组元素并显示在文本框下方,并可以删除(单击)。

Here is what I tried so far:这是我到目前为止尝试过的:

 let names = []; let nameInput = document.getElementById("name"); let messageBox = document.getElementById("display"); function insert ( ) { names.push( nameInput.value ); clearAndShow(); } function clearAndShow () { nameInput.value = ""; messageBox.innerHTML = ""; messageBox.innerHTML += "Names: " + names.join(", ") + "<br/>"; }
 <form> <h1>Please enter data</h1> <input id="name" type="text" /> <input type="button" value="Search" onclick="insert()" /> <input type="button" value="Show data" onclick="show()" /> </form> <div id="display"></div>

I'm struggling to make the little border on the output array and to delete them on click.我正在努力在输出数组上制作小边框并在单击时删除它们。 Here is how it should look like:它应该是这样的:

How it should look like它应该是什么样子

I believe you can use this site for consultation.https://www.w3schools.com/jsref/相信大家可以使用本站进行咨询。 https://www.w3schools.com/jsref/

 let names = []; let nameInput = document.getElementById("name"); let messageBox = document.getElementById("display"); function insert ( ) { names.push( nameInput.value ); clearAndShow(); } function clearAndShow () { nameInput.value = ""; messageBox.innerHTML = ""; names.forEach(function(element){ if(element != ''){ var _span = document.createElement('span'); _span.style.borderStyle = "solid" _span.style.width = '50px' _span.appendChild(document.createTextNode(element)) messageBox.appendChild(_span) } }) }
 <form> <h1>Please enter data</h1> <input id="name" type="text" /> <input type="button" value="Search" onclick="insert()" /> <input type="button" value="Show data" onclick="show()" /> </form> <div id="display"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM