在我试图解释我的问题时,请原谅我的新手的说法(而且我做了搜索,什么也没想出来,我也想使用ES6,而不是jQuery)。
我正在尝试一个文本输入,当输入时,它会被推入一个空数组中。当输入更多的输入时,我也希望将其绑定到数组中。
所以我让它起作用了。我可以在数组中得到一件东西,但我似乎没有别的东西可以进去。到目前为止,我的情况如下:
我的html:
<input type="text" class="theplayer pre" name="Player" id="bind" placeholder="Enter Names" onclick='javascript: this.value = ""' />我的JS:
let namesOfPlayers = [];
const currentValue = document.getElementById("bind").value;
namesOfPlayers.push(currentValue);
//I don't know how to add in another text/value here
console.log('namesOfPlayers', namesOfPlayers);我不知道是否应该在这里为每个值做一个,或者是否有一种方法将另一个值绑定到数组中。
谢谢并非常感谢你让我笨拙地解释这件事。
发布于 2017-04-01 01:47:58
您可以将<input type="button">元素作为<input type="text">元素的同级元素。在click on <input type="button"> .push() .value of <input type="text">数组
<input type="text"
class="theplayer pre"
name="Player"
id="bind"
placeholder="Enter Names" />
<input type="button" value="click" />
<script>
let namesOfPlayers = [];
let input = document.getElementById("bind");
let button = input.nextElementSibling;
button.addEventListener("click", function() {
namesOfPlayers.push(input.value);
console.log(namesOfPlayers);
});
</script>
https://stackoverflow.com/questions/43151793
复制相似问题