简体   繁体   English

Onchange 大量输入 JS vanilla

[英]Onchange on a lot of inputs JS vanilla

I have multiple inputs on my website.我的网站上有多个输入。 The inputs created with createElement .使用createElement创建的输入。 I can give every input class, id and etc.我可以提供每个输入类、ID 等。

My goal: every time the user inserts new input (amount of products) it will calculate the overall price without any click, just automatic calculation(object price * amount in the input).我的目标:每次用户插入新输入(产品数量)时,它都会计算总价格,无需任何点击,只需自动计算(输入中的对象价格 * 数量)。

I would post the code, but I think image will explain it better:我会发布代码,但我认为图像会更好地解释它: 在此处输入图片说明

How can I do the onchagne with getElementByTagName .我怎样才能用getElementByTagNameonchagne Example will be grate.例子将是篦子。 Thank you !谢谢 !

You can use event delegation.您可以使用事件委托。 Several events -- like "change" -- on your input elements will bubble up to container elements, up to the root of the DOM. input元素上的几个事件——比如“change”——会冒泡到容器元素,直到 DOM 的根。 So identify which is the common ancestor element, and listen there for the event you are interested in. I will choose the "input" event, as it triggers on any change the user makes, via any way of input:因此,确定哪个是共同的祖先元素,并在那里侦听您感兴趣的事件。我将选择“输入”事件,因为它会在用户通过任何输入方式进行的任何更改时触发:

document.addEventListener("input", function() {
    // make calculation here:
});

This way you only need to attach one listener.这样你只需要附加一个监听器。

You can do something like this你可以做这样的事情

function changeHandler() {
  // on change handler
}
const inputs = [...document.getElementsByTagName('input')];
inputs.forEach(input => input.addEventListener('click', changeHandler));

You can do this with classes, which is better than applying your job to all your inputs.您可以使用类来做到这一点,这比将您的工作应用于所有输入要好。 I hope is something like this do you need :我希望你需要这样的东西:

 function addInputListener(input) { //@todo Replace this by your data let row = input.parentElement.parentElement; let overallPriceElement = row.querySelector('.overall-price'); let price = parseInt(row.querySelector('.price').innerText); let calcEvent = function() { //@todo Put your calc function here if (this.value > 0) { overallPriceElement.innerText = (price * this.value).toString(); } else { overallPriceElement.innerText = "No products yet"; } }; input.addEventListener('input', calcEvent); calcEvent.call(input); // call() is here to add input context } // It's better to use class instead tag to do your job let inputs = document.querySelectorAll('.amount'); // But you can also use tag do this //let inputs = document.querySelectorAll('input'); for (let i = 0; i < inputs.length; i++) { let input = inputs[i]; addInputListener(input); }
 <table> <thead> <tr> <th>#</th> <th>Name</th> <th>Price</th> <th>Amount</th> <th>Overall Price</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>Bread</td> <td class="price">3</td> <td><input type="number" class="amount"></td> <td class="overall-price"></td> </tr> <tr> <td>2</td> <td>Bread</td> <td class="price">10</td> <td><input type="number" class="amount"></td> <td class="overall-price"></td> </tr> <tr> <td>3</td> <td>Bread</td> <td class="price">30</td> <td><input type="number" class="amount"></td> <td class="overall-price"></td> </tr> </tbody> </table>

You can call function addInputListener(input) each time you create new row pass directly your new input created by createElement to add these listeners.您可以在每次创建新行时调用函数addInputListener(input)直接传递由createElement创建的新输入以添加这些侦听器。

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

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