简体   繁体   English

两个Key Up事件,我需要将这两个事件中的数据都放入两个变量中

[英]Two Key Up events and I need data from both into two variables outside these events

  • I have two Key Up events, which log values of two different input elements. 我有两个Key Up事件,它们记录两个不同input元素的值。
  • I want to combine both values from both events into one variable. 我想将两个事件的两个值合并到一个变量中。
  • I have the following snippet. 我有以下片段。

 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> 

This is a classic case to use streams. 这是使用流的经典案例。 We can use RxJS to solve this kind of problems very easily. 我们可以使用RxJS非常轻松地解决此类问题。 Working demo 工作演示

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

相关问题 JavaScript Click事件-为什么需要两个addEventListeners? - JavaScript Click Events - Why do I need two addEventListeners? Fullcalendar:如何从两个变量中获取事件并传递给&#39;eventAfterRender&#39;? - Fullcalendar: how to get events from two variables and pass to through 'eventAfterRender'? 按Enter键时触发了两个事件 - Two Events Fired when I press enter key 如何获得两个输入事件中两个不同 function 中的两个变量的总和,并且两者都在本地 scope 中? - How to get sum of two variables which are inside two different function inside two input events and both are in local scope? 浏览器之外的关键事件? - Key events outside the browser? 事件和回调,需要等待两个事件触发并回调 - Events and Callbacks, need to wait before two events triggered and call callback 一个按钮的两个事件 - Two events from one button 在IF语句中添加两个事件…需要帮助! - Adding TWO events to an IF statement… Need help! 如何解决两个事件之间的冲突,单击外部并单击按钮以关闭 svelte kit 中的菜单? - how can I resolve a collision between two events, click outside and on the button to close a menu in svelte kit? 只有两个事件都为真时才触发一个函数的Javascript - Javascript to trigger one function only if two events are both true
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM