开发者

Force an enter event in an input with jQuery

开发者 https://www.devze.com 2023-01-30 12:09 出处:网络
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?

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

精彩评论

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