2009-09-29 29 views
3

到目前爲止,我有這個被稱爲按鍵。使輸入鍵在JavaScript中的行爲像tab鍵

function chkenter() 
{ 
    e = event.keyCode; 

    if (e == 13) 
    { 
    event.keyCode = 9; 
    return false; 
    } 
} 

但即使我將鍵碼設置爲9它不會切換到下一個控件。我怎樣才能做到這一點?我在Asp Classic工作。

我最終想要的是將焦點轉移到下一個元素。然而,下一個元素的id不是完全確定的,因爲它的id被設置爲一個循環,但是在這一點上它存在。 如何將焦點設置到下一個控件是一個問題。

+0

您標記VS2005這個問題。這讓你聽起來很像這個人一樣的問題:http://www.webdeveloper.com/forum/showthread.php?t=18598 – 2009-09-29 16:18:10

+2

@crescent這已經發布6年了...也是近2年前VS2005可用... – 2009-09-29 16:22:25

+0

@Rex:你的觀點?不同版本的VS *中的相同問題可能具有相同的分辨率。 – 2009-09-29 16:26:41

回答

1

這是一個IE瀏覽器唯一的腳本,所以如果你嘗試在Firefox上運行它不會工作。

對於IE來說,刪除「return false;」從你的代碼中,不要忘記將這個函數轉化爲「onkeydown」事件。

+0

這是正確的。謝謝。我應該意識到這一點。 onkeydown就在密鑰按下之前。謝謝!! – Eric 2009-09-29 16:33:26

0

你可以做到這一點使用jQuery像這樣:

function checkKey(e){ 
    if(e.keyCode == 13){ // if enter key 
     var e = jQuery.Event('keydown'); // create a manual event 
     e.which = e.charCode = 0; 
     e.keyCode = 9; // set the new event to "tab" 
     $(this.target).trigger(e); // trigger the new event (on the document) 
     return false; // cancels the original "enter" event from executing 
    } 
} 
// lets say you bind the event on the whole document... 
$(document).on('keydown', checkKey);