[英]How to get whether the pressed key is backspace or not in JavaScript?
I have a input text
which used to filter data in a table using the onkeyup
event 我有一个
input text
,该input text
用于使用onkeyup
事件过滤表中的数据
<input id="NameFilterText" type="text" onkeyup="return filterDataRow('NameFilterText','Name'); return false;" /></td>
I'm calling this JavaScript function in the onkeyup
to the filter data 我在
onkeyup
调用此JavaScript函数来过滤数据
function filterDataRow(field, name) {
var textBox = document.getElementById(field);
var columnName = name;
var table = document.getElementById('table1');
var headRow = table.rows[0];
var column = 0
var text = textBox.value;
for (var i = 0; i < headRow.cells.length; i++) {
var cellName = headRow.cells[i].innerHTML;
if (cellName == columnName) {
column = i;
break;
}
}
for (var i = 1; i < table.rows.length; i++) {
table.rows[i].style.display = 'table-row'; // execute only when pressing backspace
for (var v = 0; v < text.length; v++) {
var CurCell = table.rows[i].cells[column];
var CurCont = CurCell.innerHTML.replace(/<[^>]+>/g, "");
var reg = new RegExp(text + ".*", "i");
if (CurCont.match(reg) == null) {
table.rows[i].style.display = 'none';
}
}
}
return false;
}
I don't want to execute that commented line if the pressed key is not backspace
. 如果按下的键不是
backspace
,我不想执行该注释行。 How can I do that ? 我怎样才能做到这一点 ?
First you need to change the onkeyup event to this: 首先,您需要将onkeyup事件更改为此:
<input ... onkeyup="filterDataRow('NameFilterText','Name');" />
Then inside the function edit the line you want to be executed only one time adding this if-statement: 然后在函数内编辑您想仅执行一次的行,并添加以下if语句:
if (window.event.keyCode == 8) table.rows[i].style.display = 'table-row';
var input = document.getElementById('NameFilterText');
var keydown=0;
input.onkeydown = function() {
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 )
keydown=1;
return false;
};
Now in your code filterDataRow() 现在在您的代码中filterDataRow()
if(keydown=1){ do your thing. and set keydown = 0 again}
Hope it Helps ! 希望能帮助到你 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.