<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add" onclick="addRecord();">Add </button>
<button type="button" id="display" onclick="displayRecord();">Display</button>
</td>
</tr>
</table>
I need to take input from the user, when user clicks on the Add button after entering data in the Textbox. User may add 'n' number of inputs which I do not know. I need to store the inputs in an array and display it on clicking the Display button. How should that be done? I tried using push(), however it doesn't work:
当用户在文本框中输入数据后单击“添加”按钮时,我需要从用户那里获取输入。用户可以添加我不知道的'n'个输入。我需要将输入存储在一个数组中,并在单击“显示”按钮时显示它。应该怎么做?我尝试使用push(),但它不起作用:
function addRecord()
{
var add= document.getElementById("add");
var addArray= [];
addArray.push(add.value);
}
2 个解决方案
#1
2
a spaghetti solution might be as below snippet,
意大利面条解决方案可能如下所示,
values = [];
function addRecord() {
var inp = document.getElementById('inputtext');
values.push(inp.value);
inp.value = "";
}
function displayRecord() {
document.getElementById("values").innerHTML = values.join(", ");
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add" onclick="addRecord();">Add </button>
<button type="button" id="display" onclick="displayRecord();">Display</button>
</td>
</tr>
</table>
<div id='values'></div>
#2
1
"use strict";
let addButton = document.querySelector('#add');
let displayButton = document.querySelector('#display');
let records = [];
addButton.addEventListener('click', addRecord);
displayButton.addEventListener('click', displayAll);
function addRecord() {
let record = document.querySelector('#inputtext').value;
if (!record) {
return;
}
records.push(record);
document.querySelector('#inputtext').value = '';
}
function displayAll() {
alert(records);
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add">Add </button>
<button type="button" id="display">Display</button>
</td>
</tr>
</table>
#1
2
a spaghetti solution might be as below snippet,
意大利面条解决方案可能如下所示,
values = [];
function addRecord() {
var inp = document.getElementById('inputtext');
values.push(inp.value);
inp.value = "";
}
function displayRecord() {
document.getElementById("values").innerHTML = values.join(", ");
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add" onclick="addRecord();">Add </button>
<button type="button" id="display" onclick="displayRecord();">Display</button>
</td>
</tr>
</table>
<div id='values'></div>
#2
1
"use strict";
let addButton = document.querySelector('#add');
let displayButton = document.querySelector('#display');
let records = [];
addButton.addEventListener('click', addRecord);
displayButton.addEventListener('click', displayAll);
function addRecord() {
let record = document.querySelector('#inputtext').value;
if (!record) {
return;
}
records.push(record);
document.querySelector('#inputtext').value = '';
}
function displayAll() {
alert(records);
}
<table>
<tr>
<td>Enter the Input</td>
<td><input type="text" id="inputtext" /></td>
</tr>
<tr>
<td></td>
<td><button type="button" id="add">Add </button>
<button type="button" id="display">Display</button>
</td>
</tr>
</table>