[英]If I have 45 buttons (numbers) and I have to choose 6 numbers, how can I make my 6 clicked buttons go to my 6 inputs numbers
If I have 45 buttons (numbers) and I have to choose 6 numbers, how can I make my 6 clicked buttons go to my 6 inputs numbers, that is I mean for each click on a button, send this value to an input type number 如果我有45个按钮(数字)并且必须选择6个数字,那么如何使6个单击的按钮转到6个输入数字,即我的意思是每次单击按钮,就将此值发送给输入类型数字
I clicked on the first button and I did the function of calling the input id = "chk1" to the first inbox innerHTML.value = "", but how do I make the second clicked button go to the second input? 我单击了第一个按钮,然后执行了将输入id =“ chk1”调用到第一个收件箱innerHTML.value =“”的功能,但是如何使第二个单击的按钮转到第二个输入呢?
21 22 21 22
<script type="text/javascript">
function botonear() {
document.getElementById("chk1").value= 21;
}
function botonear2() {
document.getElementById("chk2").value= 22;
}
</script>
I expect click in any button and send it to the first empty input, then second click in different button goes to the second empty input and so.. 我希望单击任何按钮并将其发送到第一个空输入,然后第二次单击不同的按钮进入第二个空输入,依此类推。
Assuming you have myButtons
and myInputs
as arrays of your buttons and inputs respectively, you can: 假设将
myButtons
和myInputs
作为按钮和输入的数组,则可以:
1) use using addEventListener
to attach event listeners to each button and detect clicks; 1)使用
addEventListener
将事件侦听器附加到每个按钮并检测点击; and 和
2) use array.prototype.find
to find your first empty input and update its value. 2)使用
array.prototype.find
查找您的第一个空输入并更新其值。
myButtons.forEach((button, i) =>
button.addEventListener('click', () => {
let firstEmptyInput = myInputs.find(input => !input.value);
if (firstEmptyInput)
firstEmptyInput.value = i;
}));
[1] addEventListener
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener [1]
addEventListener
https://developer.mozilla.org/zh-CN/docs/Web/API/EventTarget/addEventListener
[2] find
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find ` [2]
find
: https : // developer.mozilla.org/ zh- CN/ docs/ Web/ JavaScript/Reference/ Global_Objects/ Array/ find`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.