开发者

Force an enter event in an input with jQuery

I have an input which changes value when a click a button. What I want to do is to force an enter keyp开发者_如何学Goress event with jQuery. without pressing the button. Is this possible?

Thanks


jQuerys .trigger() should do the trick:

$('input').trigger({
    type: 'keypress',
    which: 13
});

Ref.: .trigger()


$(element).keydown(function(event) { if (event.keyCode == '13') { event.preventDefault(); } do_something(); });


I needed to use this on a search box to clear it's content...but instead of using 'keypress' or 'keydown', I needed to use 'keyup' so that the search function would fire also. This way I could clear the limiting search results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜