【问题标题】:Is there a way to implement on keyup event except one key?除了一个键,有没有办法在 keyup 事件上实现?
【发布时间】:2017-10-12 12:39:57
【问题描述】:

我正在尝试开发一个 Javascript 函数,该函数在每次输入更改时都会进行一些验证(我正在使用 onkeyup 事件),除非我们删除了我们在输入中输入的内容。这是我的实际代码:

function myFunction() {
var input, filter, table, tr, td, i;
var cpt = 0;
var nbRow = 0;
input = document.getElementById("filterInput");
filter = input.value.toUpperCase();
table = document.getElementById("test");
thead = table.getElementsByTagName("tbody");
tr = table.getElementsByTagName("tr");

for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[2];

    if (td) {
        nbRow = nbRow + 1;
        if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
        } else {
            tr[i].style.display = "none";
            cpt = cpt + 1;

        }
    }
}
if (nbRow == cpt) {
   alert("The list is empty")
}
}
<input id="filterInput"  onkeyup="myFunction()">
<table id="test">
<thead>
<tr>
<th>Titre1</th>
<th>Titre2</th>
<th>Titre3</th>
</tr>
</thead>
<tbody>
<tr>
<td>contenu1</td>
<td>contenu2</td>
<td>contenu3</td>
</tr>
</tbody>

</table>

如何避免每次用户删除一个字符时出现重复的alert 显示?

编辑:

我试图避免在用户删除一个字符后重复“警告”而不丢失验证。

【问题讨论】:

标签: javascript html dom-events


【解决方案1】:

如果您想使用 onkeyup 忽略删除和退格,可以将此检查添加到函数的开头:

function myFunction(event) {
    // capture what key was pressed
    var key = event.keyCode || event.charCode;

    if( key === 8 || key === 46 )
        return; //if it's del or backspace, exit the function

    // continue your function here
}

【讨论】:

    【解决方案2】:

    您可以在检测按下哪个键的函数中实现验证...

    function myFunction(e) {
       if(e.keyCode !== 8){ // back key excluded
             var input, filter, table, tr, td, i;
    				var cpt = 0;
    				var nbRow = 0;
    				input = document.getElementById("filterInput");
    				filter = input.value.toUpperCase();
    				table = document.getElementById("test");
    				thead = table.getElementsByTagName("tbody");
    				tr = table.getElementsByTagName("tr");
    
    				for (i = 0; i < tr.length; i++) {
    					td = tr[i].getElementsByTagName("td")[2];
    
    					if (td) {
    						nbRow = nbRow + 1;
    						if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
    							tr[i].style.display = "";
    						} else {
    							tr[i].style.display = "none";
    							cpt = cpt + 1;
    
    						}
    					}
    				}
    				if (nbRow == cpt) {
    alert("The list is empty")
    				}
       }
    				
    }
    <input id="filterInput"  onkeyup="myFunction(event)">
    <table id="test">
    <thead>
    <tr>
    <th>Titre1</th>
    <th>Titre2</th>
    <th>Titre3</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>contenu1</td>
    <td>contenu2</td>
    <td>contenu3</td>
    </tr>
    </tbody>
    
    </table>

    当然,您必须为要排除的每个键添加代码

    【讨论】:

    • 您可能会在移动设备上遇到问题。也许更好地检查最后一个事件的字符串长度。
    • 它在移动版 Firefox 上运行良好!!为什么你不这么认为?
    • 简单高效!只有一件事,当您删除时,您将失去验证!
    • 我在某些移动浏览器中遇到了缺少 keyCode 的问题。小心点
    • @kevinSpaceyIsKeyserSöze 谢谢!!
    猜你喜欢
    • 2019-08-08
    • 2012-05-19
    • 2019-12-13
    • 2021-12-31
    • 1970-01-01
    • 2011-09-10
    • 2020-03-19
    • 2011-11-22
    • 1970-01-01
    相关资源
    最近更新 更多