我想在有焦点的表格行上按下按键时调用 java 脚本函数。下面是代码,但是当我按 Enter 键时,不会调用脚本函数。
http://jsfiddle.net/sirishkumar/58FZG/19/
<input id="test" type="text">
<table>
<tr onkeypress="return openLog(e,'row 1')">
<td>Row 1</td>
</tr>
<tr onkeypress="return openLog(e,'row 2')">
<td>Test</td>
</tr>
</table>
var j = jQuery;
var currentRow = 0;
var pagesize = 2;
function ChangeCurrentRow() {
var tableRow = document.getElementsByTagName("tr")[(currentRow % pagesize)];
tableRow.focus();
j(tableRow).siblings().removeClass("highlight-row");
j(tableRow).addClass("highlight-row");
}
j(document).ready(function () {
j('#test').val("Ready");
ChangeCurrentRow();
});
j(document).keydown(function (e) {
if (e.keyCode == 38) {
currentRow--;
ChangeCurrentRow();
return false;
}
if (e.keyCode == 40) {
currentRow++;
ChangeCurrentRow();
return false;
}
});
function openLog(e, id) {
if (e.keyCode == 13) {
$('#input').text(id)
}
return true;
}