我想在具有焦點的錶行上按鍵時調用java腳本函數。下面是代碼,但是當我按下Enter鍵時,腳本函數沒有被調用。爲處理焦點行處理onkeypress事件
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;
}
你確定你使用了正確的keyCode嗎?我檢查了一下,keydown事件實際上是在發射。 – sweetamylase 2013-03-08 09:31:37
是http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes。 – Poorna 2013-03-08 09:36:11
@sweetamylase如何在Firebug中查看關鍵事件?您是否使用不同的工具 – Poorna 2013-03-08 09:39:01