2017-08-08 6 views
0

私のページの再読み込みを行いたいのですが、値の検索(localStorageのストック)を入力に設定してから、 (タイプ13)。reloadのinput.val()とfocus()の後のkeypressが機能しない

は、ここに私のコードです:

$(function() { 

if((localStorage.getItem("search")) && (sessionStorage.getItem("reloadAfterPageLoad")!="false")) 
{ 
    $('#datatable_filter input').val(localStorage.getItem("search")).focus().trigger(jQuery.Event('keypress', { keycode: 13, which: 13, charCode: 13 })); 


} 
}); 

フォーカス()( "検索")localStorage.getItemとして動作しますが、私のイベントは決して起こりません。

$(function() { 

if((localStorage.getItem("search")) && (sessionStorage.getItem("reloadAfterPageLoad")!="false")) 
{ 
var e = jQuery.Event("keypress"); 
e.which = 13; 
alert("la recherche était : "+localStorage.getItem("search")); 
$('#datatable_filter input').val(localStorage.getItem("search")).focus().trigger(e); 
sessionStorage.setItem("reloadAfterPageLoad", "false"); 

} 

をそしてそれはまた、作品をしない:

私はすでに、jQueryを使ってキープレスイベントをトリガするために決定的な方法の解決策を試してみてください。

問題についてご意見はありますか? お返事ありがとうございます

+0

そして、そのような 'keypress'をトリガすることによって、起こることになっているもの、形が提出すべきか、何? – adeneo

+0

[jQueryでキー入力イベントをトリガするための決定的な方法](https://stackoverflow.com/questions/832059/definitive-way-to-trigger-keypress-events-with-jquery) – user1777136

+0

@ adeneo21:テーブルは私のデータにフィルタが適用されるので、リフレッシュする –

答えて

0

入力のキー入力/変更ロジックを別の関数に移動して呼び出すことができます。

$('input').keypress(function(event) { 
 
    updateTable(event); 
 
}); 
 

 
function updateTable(event) { 
 
    //update the table 
 
} 
 

 
#onLoad 
 
$(function() { 
 
    if ((localStorage.getItem("search")) && (sessionStorage.getItem("reloadAfterPageLoad") != "false")) { 
 
    $('#datatable_filter input').val(localStorage.getItem("search")).focus(); 
 
    updateTable(); 
 
    } 
 
});

関連する問題