简体   繁体   English

如果我有45个按钮(数字)并且必须选择6个数字,我该如何使6个单击的按钮转到6个输入数字

[英]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: 假设将myButtonsmyInputs作为按钮和输入的数组,则可以:

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] findhttps : // developer.mozilla.org/ zh- CN/ docs/ Web/ JavaScript/Reference/ Global_Objects/ Array/ find`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何制作带有 0-9 数字的按钮,当我按下它们时添加到文本框中 - how can i make buttons with numbers from 0-9 that adds into a textbox when i press them 如何从数组中减去数字以使总数为 0。我想跳过会使总数为负数的数字 - How can I subtract numbers from an Array to get my total to 0. I want to skip numbers that would make my total a negative 我的 React 应用程序中有按钮和教科书,如何使单击按钮触发文本输入? - I have buttons and a textbook in my React App, How do I make the click of the button trigger text input? 您是否知道为什么我的代码无法正常工作,而单击时却无法获取数字? - Do you have any idea why my code does not work and I can not get the numbers while clicking 如何在 HTML 的有序列表中使用自定义格式的数字? - How can I have numbers with a custom format in an ordered list in HTML? Codewars问题“快乐数字”我如何才能对我的代码进行一些更改以使其起作用? - Codewars problem 'Happy numbers' how can i make some changes on my code to make it works? 如何在幻灯片中添加可点击的数字? - How can I add clickable numbers to my slides? 如何从网页上将负数更改为0 - How can i change the negative numbers to 0 from my webpage 我怎样才能让它显示我的参数中所有数字的索引 - How can I get this to show the index of all the numbers in my parameter 我怎样才能让我的计数器停在 0? 它递减为负数 - How can I get my counter to stop at 0? It decrements to negative numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM