[英]how can i make buttons with numbers from 0-9 that adds into a textbox when i press them
[英]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
如果我有45個按鈕(數字)並且必須選擇6個數字,那么如何使6個單擊的按鈕轉到6個輸入數字,即我的意思是每次單擊按鈕,就將此值發送給輸入類型數字
我單擊了第一個按鈕,然后執行了將輸入id =“ chk1”調用到第一個收件箱innerHTML.value =“”的功能,但是如何使第二個單擊的按鈕轉到第二個輸入呢?
21 22
<script type="text/javascript">
function botonear() {
document.getElementById("chk1").value= 21;
}
function botonear2() {
document.getElementById("chk2").value= 22;
}
</script>
我希望單擊任何按鈕並將其發送到第一個空輸入,然后第二次單擊不同的按鈕進入第二個空輸入,依此類推。
假設將myButtons
和myInputs
作為按鈕和輸入的數組,則可以:
1)使用addEventListener
將事件偵聽器附加到每個按鈕並檢測點擊; 和
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/zh-CN/docs/Web/API/EventTarget/addEventListener
[2] find
: https : // developer.mozilla.org/ zh- CN/ docs/ Web/ JavaScript/Reference/ Global_Objects/ Array/ find`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.