gpt4 book ai didi

javascript - 使用来自输入的数据更新数组

转载 作者:行者123 更新时间:2023-11-30 14:15:17 26 4
gpt4 key购买 nike

我有几个输入,我正在复制 n 次,我正尝试从数组中的输入添加 数值。我将单词标记为“添加”,因为数组可能已被其他数字填充。

我正在尝试在此处应用 UncleDave 的 回答中的方法: JavaScript - Add Value from input box to array

示例:

我有一个数组:

var exampleArray = [[1, 1.5], [1, 1], [0, 25.5]];

我做了什么:

在第一个输入中写入值 25。在第二个输入中写入值 1.5。

创建两个新输入。

在第一个输入中写入值 25.4。在第二个输入中写入值 1。

按下添加到数组的按钮。

我想要达到的目标:

var exampleArray = [[1, 1.5], [1, 1], [0, 25.5], [25, 1.5], [25.4, 1]];

我达到了什么:

控制台日志中的“Udefined”。

这是带有我的代码的 jsfiddle 链接: https://jsfiddle.net/aectann/k3qwoz0g/12/

更新了片段(好吧,这时候并不难,MTCoster,谢谢你的建议):

var totalInputs;
var myInputs;
var tmpARR = [];
var count = 0,
types = ['t', 'C' /*, 'Q'*/ ],
button = document.getElementById('button');

button.addEventListener("click", createInputs, false);

function createInputs() {
if (!validInput()) {
return;
}
count += 1;
createInput(count);
}

function createInput(count) {
totalInputs = document.getElementsByClassName('myInput').length;
var existingNode = document.getElementsByClassName('myInput')[totalInputs - 1];

types.forEach(function(type) {
var newNode = existingNode.cloneNode();
newNode.value = null;
newNode.id = type + +count;
newNode.placeholder = 'Placeholder ' + type;
newNode.dataset.id = 'id' + count;
appendNode(newNode);

})
}

function appendNode(node) {
document.querySelector('#div').appendChild(node);
}

function validInput() {
myInputs = document.getElementsByClassName('myInput');
var valid = true;

Array.prototype.slice.call(myInputs).forEach(function(input) {

input.classList.remove('error');
if (!input.value) {
input.classList.add('error');
valid = false;
}
});

return valid;
}

function removeError(event) {
event.classList.remove('error');
}

function guardarNumeros() {
boxvalue = document.getElementsByClassName('myInput').value;
tmpARR.push(boxvalue);
console.log(tmpARR);
return false;
}
#title {
font-family: 'Times New Roman', Times, serif;
font-size: 200%;
}

#step {
font-size: 15pt;
clear: both;
}

#step2 {
font-size: 15pt;
clear: both;
}

#step3 {
font-size: 15pt;
clear: both;
}

summary {
background: #009688;
color: #fff;
padding: 5px;
margin-bottom: 3px;
text-align: left;
cursor: pointer;
padding: 5px;
width: 250px;
/*background-color: #4CAF50;*/
}

summary:hover {
background: #008999;
}

.displayBlockInline-Flex {
display: inline-flex;
}

#margin20 {
margin-left: 20px;
vertical-align: middle;
}

#container {
width: auto;
height: auto;
margin: 0 auto;
display: none;
}

a.myButton {
color: #fff;
/* цвет текста */
text-decoration: none;
/* убирать подчёркивание у ссылок */
user-select: none;
/* убирать выделение текста */
background: rgb(212, 75, 56);
/* фон кнопки */
outline: none;
/* убирать контур в Mozilla */
text-align: center;
cursor: pointer;
width: 150px;
padding-bottom: 11px;
}

a.myButton:hover {
background: rgb(232, 95, 76);
}


/* при наведении курсора мышки */

a.myButton:active {
background: rgb(152, 15, 0);
}


/* при нажатии */

.button1 {
/* background-color: #fc0; /* Цвет фона слоя */
/* padding: 5px; /* Поля вокруг текста */
float: left;
/* Обтекание по правому краю */
width: 450px;
/* Ширина слоя */
}

.button2 {
/* background-color: #c0c0c0; /* Цвет фона слоя */
/* padding: 5px; /* Поля вокруг текста */
width: 650px;
/* Ширина слоя */
float: right;
/* Обтекание по правому краю */
}

.clear {
clear: left;
/* Отмена обтекания */
}

.wrapper {
width: 1100px;
margin-left: 20px;
}


/*inputs*/

#div {
text-align: center;
}

.myInput {
height: 40px;
outline: none;
width: auto;
border: 1px solid #999;
border-radius: 4px;
padding: 5px 10px;
margin: 5px;
display: inline-block;
}

.myInput.error {
border: 1px solid red;
}

#action {
margin: 10px 0;
text-align: center;
}

#button {
width: 190px;
height: 40px;
background: #009688;
color: #fff;
font-weight: 600;
font-size: 13px;
border-radius: 4px;
border: none;
/* text-transform: uppercase;*/
outline: none;
cursor: pointer;
}

#button:hover {
background: #008999;
}
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

<center>
<input type="text" class="myInput" name="nameAlloy" placeholder="Name">
</center>
<div id="div">
<!--<form onsubmit="return guardarNumeros()">-->
<div id="action">
<button type="button" id="button">Add more inputs</button>
</div>
<input type="number" onkeypress="removeError(this)" class="myInput" data-id="id0" name="value[]" placeholder="Enter value 1">
<input type="number" onkeypress="removeError(this)" class="myInput" data-id="id0" name="value[]" placeholder="Enter value 2">

<div id="action">
<input type="submit" id="button" value="Add to array">
</div>
<!--</form>-->
</div>

最佳答案

The getElementsByClassName() method returns a collection of all elements in the document with the specified class name, as a NodeList object.

您可以遍历所有数字输入的集合并更新您的结果。但我建议为数字输入创建另一个类,这样您就不需要检查输入的类型并使您的代码保持通用。

大家可以试试这段代码,有疑惑的可以在评论区留言。

function guardarNumeros() {
boxvalue = document.getElementsByClassName('myInput');
i = 0;
while (i < boxvalue.length) {
if (boxvalue[i].type == "number") {
if (boxvalue[i+1] && boxvalue[i+1].type == "number") {
tmp = [boxvalue[i].value, boxvalue[i+1].value]
tmpARR.push(tmp);
i+=2;
}
} else {
i++;
}
}
console.log(tmpARR);
return false;
}

关于javascript - 使用来自输入的数据更新数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53682294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com