简体   繁体   English

在 onClick 中每个值属性的 JavaScript 输入文本

[英]JavaScript Input text per value attribute in onClick

I wanted to input the number which I click to the text box from the attribute value present on that number.我想从该数字上存在的attribute值中将我单击的数字inputtext box

If I click number 1 then the value is number 1 and so on.如果我单击数字1,则该值为数字1 ,依此类推。

This is my script:这是我的脚本:

 function myFunc() { var dadada = document.getElementsByTagName("a"); var i; for (i = 0; i < dadada.length; i++) { var de = dadada[i].getAttribute("value"); } document.getElementById("text_id").value = de; }
 <div id="xxx"> <a class="list-group-item" href="#" value="1" onclick="myFunc()">1</a> <a class="list-group-item" href="#" value="2" onclick="myFunc()">2</a> <a class="list-group-item" href="#" value="3" onclick="myFunc()">3</a> <a class="list-group-item" href="#" value="4" onclick="myFunc()">4</a> this value: <input type="text" id="text_id" value="">

You need to get the value of the clicked link.您需要获取点击链接的值。 Get all the <a> in javascript using querySelectorAll() and then loop through them and addEventListener to them.使用querySelectorAll()获取 javascript 中的所有<a> ,然后遍历它们并向它们addEventListener

Use the eventObject e and get the attribute value of the clicked link使用 eventObject e获取点击链接的属性value

 const items = document.querySelectorAll('.list-group-item'); items.forEach(item => { item.addEventListener('click', e => myFunc(e)) }) function myFunc(e) { document.getElementById("text_id").value =e.target.getAttribute('value') }
 <div id="xxx"> <a class="list-group-item" href="#" value="1">1</a> <a class="list-group-item" href="#" value="2">2</a> <a class="list-group-item" href="#" value="3">3</a> <a class="list-group-item" href="#" value="4">4</a> this value: <input type="text" id="text_id" value="">

You can implement this by passing the dom element directly to the function like this:您可以通过将 dom 元素直接传递给函数来实现这一点,如下所示:

 function myFunc(elem) { var de = elem.getAttribute("value"); document.getElementById("text_id").value = de; return false; }
 <div id="xxx"> <a class="list-group-item" href="#" value="1" onclick="myFunc(this)">1</a> <a class="list-group-item" href="#" value="2" onclick="myFunc(this)">2</a> <a class="list-group-item" href="#" value="3" onclick="myFunc(this)">3</a> <a class="list-group-item" href="#" value="4" onclick="myFunc(this)">4</a> <br/><br/> This value: <input type="text" id="text_id" value="">

Check my answer below在下面检查我的答案

HTML HTML

<div id="parent">
  <a  class="list-group-item" value="1" href="#" >1</a> 
  <a  class="list-group-item" value="2" href="#" >2</a> 
  <a  class="list-group-item" value="3" href="#" >3</a> 
  <a  class="list-group-item" value="4" href="#" >4</a>
</div>

this value: <input type="text" id="text_id" value="">

Javascript Javascript

var parent = document.querySelector("#parent");
var input = document.querySelector("#text_id");

parent.addEventListener("click", function(e){
        input.value = e.srcElement.getAttribute("value");
})

Mine is a bit different because it uses event delegation, ie.我的有点不同,因为它使用事件委托,即。 you only need to listen to parent elements click rather than each anchor element's click.你只需要听父元素的点击而不是每个锚元素的点击。 I personally feel it's a cleaner approach and efficient.我个人认为这是一种更清洁且高效的方法。

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

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