[英]Two Key Up events and I need data from both into two variables outside these events
input
elements. 我有两个Key Up事件,它们记录两个不同input
元素的值。 var textInputSVK = document.getElementById("top__itextsvk"); var textInputKg = document.getElementById("top__itextkg"); function delay(e, delay) { let timer = 0; return function(...args) { clearTimeout(timer); timer = setTimeout(e.bind(this, ...args), delay || 0); }; } textInputKg.addEventListener( "keyup", delay(e => { var t = textInputKg.value; console.log(t); }, 500) ); textInputSVK.addEventListener( "keyup", delay(e => { var t = textInputSVK.value; console.log(t); }, 500) );
<input id="top__itextkg" type="text" value="10" autocomplete="off" /> <input id="top__itextsvk" value="10" autocomplete="off" type="text" />
We can do it using Event Bubbling
in which we will enclose the two input elements into a div and then identify the child element in which Key Up event is pressed. 我们可以使用Event Bubbling
来做到这一点,在Event Bubbling
中,我们将两个输入元素封装到div中,然后确定在其中按下Key Up事件的子元素。
var parent = document.getElementById("parent"); function delay(e, delay) { let timer = 0; return function(...args) { clearTimeout(timer); timer = setTimeout(e.bind(this, ...args), delay || 0); }; } parent.addEventListener( "keyup", delay(e => { var t = e.target.value; console.log(`this event is from ${e.target.id}`,t); }, 500) );
<div id="parent"> <input id="top__itextkg" type="text" value="10" autocomplete="off" /> <input id="top__itextsvk" value="10" autocomplete="off" type="text" /> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.