[英]how to trigger enter event in text box without pressing enter in javascript
[英]how to trigger a function by pressing enter?
我有此按鈕可導致javascript中的一種方法來過濾表格信息:
<button type="button" id="search" title="Buscar" onclick="fn_cliente.filtraInformacion();"></button>
有幾個字段可以過濾信息,我希望在任何輸入上按“輸入”時,它執行與單擊按鈕時相同的方法
使用以下命令將enter事件綁定到您的搜索字段。
document.querySelectorAll('.searchfield').forEach(function(input) {
input.addEventListener('keypress', function(e) {
if (e.keyCode === 13) filtraInformacion();
});
});
function filtraInformacion() { console.log('filtraInformacion has been called!'); } document.querySelectorAll('.searchfield').forEach(function(input) { input.addEventListener('keypress', function(e) { if (e.keyCode === 13) filtraInformacion(); }); });
<button type="button" id="search" title="Buscar" onclick="filtraInformacion();">Click me</button> <br> <input id='name' class='searchfield' placeholder='Press Enter!'> <br> <input id='lastname' class='searchfield' placeholder='Press Enter!'><br> <input id='gender' class='searchfield' placeholder='Press Enter!'>
看到? 當按下回車鍵時,將調用filtraInformacion
函數。
這可行。 您可以將其調整為所需的任何元素。
<input type="text" id="account-location-country-input" class="mdc-text-field__input" required onkeyup = "if (event.keyCode == 13)document.getElementById('edit-account-button').click()">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.