2012-01-30 14 views
0

"s"を押した後、メインの検索バーの入力にフォーカスしたいと思います。JS:入力をフォーカスするKeyShortcut

私がしている:

$("body").keypress(function(event) { 
    if(do_focus == 1 && event.which == 115) 
    { 
     $('#search-global-input').focus(); 
    } 
}); 

それは正常に動作していますが、「S」を押した後、私は入力ではなく、その「S」の文字としています。 「s」文字をどうやって取得するのですか? :)

私は

は、あなたがのデpreventDefaultメソッドを使用する必要がありますhttp://api.jquery.com/event.preventDefault/

答えて

1

をチェック

1
$("body").keypress(function(event) { 
    if(do_focus == 1 && event.which == 115) 
    { 
     $('#search-global-input').focus(); 
     event.preventDefault(); // this stops the default behavior 
    } 
}); 

ありがとう.. SUBSTRを介したが、結果なしでそれをカット、文字列をロードするためにtryied実際のイベントを止め、競合を起こさずにしたいことをするためのJquery ...(リダイレクションを防ぐためにかなり良い要素)

$('#something').keypress(function (event) { 
    event.preventDefault(); 
    // your stuff 
}); 
関連する問題