首页 > 解决方案 > 如何在 Angular JS (1.6) 驱动的网站中预填充数据时触发手动输入事件

问题描述

需要预填充数据并确保它通过验证。目前,当用户手动输入数据时会触发验证。

标签: javascriptangularjs

解决方案


我们可以使用以下代码实现结果。

     const elem = document.getElementById('mat-input-0'); 
     elem.value = 'smtomymail@gmail.com'; 
     elem.dispatchEvent(new Event('input')); 
     elem.dispatchEvent(new Event('change')); 

     const elem1 = document.getElementById('mat-input-2'); 
     elem1.value = 'Password123'; 
     elem1.dispatchEvent(new Event('input')); 
     elem1.dispatchEvent(new Event('change'));

     const elem2 = document.getElementById('mat-input-3');
     elem2.value = 'Password123'; 
     elem2.dispatchEvent(new Event('input')); 
     elem2.dispatchEvent(new Event('change'));

推荐阅读