2012-02-26 48 views
1

代碼:的Jquery ::按鍵時,並專注於一個隱藏的DIV

$(document).ready(function(){ 
$(".div").hide(); 
$(document).on('keypress paste', function() { 
    $('form').focus(); 
    $(".div").show("fast"); 
}); 
}); 

Thanks to adeneo for the cleaner code. 

使用說明:

DIV元素包含隱藏搜索表單。當按下鍵或在頁面上使用粘貼(ctrl + v)功能時,會出現一個搜索框,其中包含數據。

問題:

第一次按鍵的作品,但作爲按鍵取消隱藏DIV的第一個字母翻譯丟失。所以'你好'在搜索框中成爲'你好'。

第二個作品也是,只要你按ctrl + v兩次。如上所述,它使用第一個實例來調用DIV(我假設?)。

幫助:

尋找任何方式來解決這個代碼,以便它可以作爲解釋,或者爲我應該用什麼樣的代碼,如果當前的代碼將無法正常工作指引正確的方向。

預先感謝您。

回答

0

這應該適用於按鍵問題。

$(document).ready(function(){ 
$(".div").hide();      
$(document).keypress(function (e) { 
    if($('.div').is(':hidden')){ 
     $(".div").show("fast"); 
     $("#query").val($("#query").val()+String.fromCharCode(e.charCode)); 
     document.forms['form'].elements['query'].focus(); 
    } 
}); 

$(document).bind('paste', function(e){ 
    console.log(e); 
    document.forms['form'].elements['query'].focus(); 
    $(".div").show("fast"); 

}); 
}); 

http://jsfiddle.net/Jkcj6/2/

+0

感謝亞歷克斯。這是完美的。是否有任何解決方法粘貼功能? – Rob 2012-02-26 20:37:49