简体   繁体   English

如何通过单击将按钮的值添加到文本框?

[英]How can I add the value from button to the text box by click?

 const button = document.getElementById('#1'); button.addEventListener('click', e=>{ e.preventDefault() document.getElementByName('field).innerText = 1; })
 <div class="header"> <input type="text" name="field" id="field"> </div> <div class="btn"><input type="button" id="1" class="button" value="1"></div> <div class="btn"><input type="button" id="2" class="button" value="2"></div> <div class="btn"><input type="button" id="3" class="button" value="3"></div>
when I click on the button nothing be occur and how can I manage that. 当我点击按钮时什么也没有发生,我该如何管理。

A few things to learn here.在这里学习一些东西。 getElementById takes a string with no # as an argument. getElementById接受一个没有#的字符串作为参数。 You also had a typo in your getElementByName('field) (missing single quote).您的getElementByName('field)也有拼写错误(缺少单引号)。 Additionally, there isn't a getElementByName - it would be getElementsByName (plural).此外,没有getElementByName - 它将是getElementsByName (复数)。 Finally, you want to apply the same listener to all buttons of a certain class, so you can gather them up (I used document.querySelectorAll('.button') but getElementsByName("button") would work as well).最后,您希望对某个类的所有按钮应用相同的侦听器,以便将它们收集起来(我使用了document.querySelectorAll('.button')getElementsByName("button")也可以)。 Once you have that collection, iterate through it to set the event listener.拥有该集合后,请遍历它以设置事件侦听器。

Lastly when you want to change the text in an input tag, you do it through the value , not the innterText .最后,当您想更改 input 标签中的文本时,您可以通过value ,而不是通过innterText innerText is used for non-form HTML elements innerText用于非表单 HTML 元素

 const buttons = document.querySelectorAll('.button'); const field = document.querySelector('[name=field]'); buttons.forEach(button => button.addEventListener('click', e => { e.preventDefault() let f = field.value.trim()? field.value.trim().split(",") : []; f.push(e.target.value); field.value = f.join(",") }))
 <div class="header"> <input type="text" name="field" id="field"> </div> <div class="btn"><input type="button" id="1" class="button" value="1"></div> <div class="btn"><input type="button" id="2" class="button" value="2"></div> <div class="btn"><input type="button" id="3" class="button" value="3"></div>

When you are trying to get the value of a DOM element, you need to get the element reference and then use the value property.当您尝试获取 DOM 元素的值时,您需要获取元素引用,然后使用value属性。 For example:例如:

const buttonValue = document.getElementById('submit').value;

When using getElementById you don't need to use #1 , just 1 would be enough.使用getElementById您不需要使用#1 ,只需1就足够了。 Also, getElementByName doesn't seem to be a proper DOM manipulation function, the alternative is getElementsByName (gets list of all elements with specified name).此外, getElementByName似乎不是一个合适的 DOM 操作函数,替代方法是getElementsByName (获取具有指定名称的所有元素的列表)。

Keeping this in mind, see code below:记住这一点,请参阅下面的代码:

 const button = document.getElementById('1'); button.addEventListener('click', e=>{ e.preventDefault(); document.getElementById('field').value = e.target.value; });
 <div class="header"> <input type="text" name="field" id="field"> </div> <div class="btn"> <input type="button" id="1" class="button" value="1"> </div> <div class="btn"><input type="button" id="2" class="button" value="2"></div> <div class="btn"><input type="button" id="3" class="button" value="3"></div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM