简体   繁体   English

如何在没有窗口刷新的情况下在值更改时捕获输入类型“文本”上的事件?

[英]How to catch the event on input type 'text' on value change without the window refresh?

As whenever I select any value from dropdown list, the page is refreshed(window element is clicked which I dont want). 每当我从下拉列表中选择任何值时,页面都会刷新(单击窗口元素,我不想要)。

1st TS file dynamically inserting input field value when the element is selected from dropdown 从下拉列表中选择元素时,第一个TS文件动态插入输入字段值

private inputboxTrayselectionString: string = "*[data-type='tag']";       
base.$inputbox = base.$rootElement.find(base.inputboxTrayselectionString).eq(0);
base.$inputbox.val(dataArr.toString()).change();

$(window).click(function(e) {
   console.log('window clicked'); 
   base.$optionTray.hide();
});

=> 2nd TS file catching the event =>第二个TS文件捕获事件

this.filterElByTagnew = $(this.rootElementSelectionString + ' input[data-type="tag"]');   
this.filterElByTagnew.on('change', function (e) { self.cummulativeOnChangeHandler();}); 

HTML to show the dropdown 用于显示下拉列表的HTML

<div class="eG2Col blog-inline-form">
   <label>Filter:</label>
   <div class="js-multiselectDropdown">
      <div style="background-color: white">
         <div class="action-btn " data-action="colex">    <span>Choose Tag</span></div>
         <input type="text" data-type="tag" >
         <div class="eG2Col list-show" data-action="option-tray">
            <ul></ul>
         </div>
         <div data-action="checkbox-tray" class="hidden">
            <input type="checkbox" value="5G" />5G</br>
            <input type="checkbox" value="Connectivity"  />Connectivity</br>
            <input type="checkbox" value="Digital transformation" />Digital transformation</br>
            <input type="checkbox" value="Radio system"  />Radio system</br>
            <input type="checkbox" value="Mobile"  />Mobile</br>
         </div>
      </div>
    </div>
 </div>

It makes sense that you click the dropdown to interact with it. 单击下拉列表与其进行交互是有意义的。 Through a mechanism known as event bubbling , this click counts as a click on the window, because the window is an ancestor of the dropdown. 通过称为事件冒泡的机制,此单击计为窗口上的单击,因为该窗口是下拉列表的祖先。

Handle the click event on the dropdown and call stopPropagation to prevent event handlers on ancestors from running. 处理下拉列表中的click事件并调用stopPropagation以防止祖先上的事件处理程序运行。

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

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