最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Trigger enter key press automatically into a text field when page is loaded using javascript - Stack Overflow

programmeradmin1浏览0评论

I want to trigger enter key pressed in a text field when a page will be loaded.Suppose when user will load the page a text-field will trigger automatically enter key press event means the text field function should remain same as enter key pressed on keyboard.I have a text filed given below.

<input type="text"  id="txt-field" >

Manually when user is pressing enter key form key-board one function is executing which is given below.

function scan(){
alert('hello');
}

But I need this function will execute when page will be load and this text field trigger with enter key event.

I want to trigger enter key pressed in a text field when a page will be loaded.Suppose when user will load the page a text-field will trigger automatically enter key press event means the text field function should remain same as enter key pressed on keyboard.I have a text filed given below.

<input type="text"  id="txt-field" >

Manually when user is pressing enter key form key-board one function is executing which is given below.

function scan(){
alert('hello');
}

But I need this function will execute when page will be load and this text field trigger with enter key event.

Share Improve this question asked Apr 21, 2015 at 11:11 rajat_474rajat_474 3481 gold badge4 silver badges15 bronze badges 1
  • call your function on page load – Tushar Commented Apr 21, 2015 at 11:16
Add a ment  | 

1 Answer 1

Reset to default 3

You can trigger a keypress-event for a specific key when you construct the jQuery Event Object manually. You can assign an event and some properties which will be set on this object.

var e = jQuery.Event( "keydown", { keyCode: 13 } );
$("#txt-field").trigger(e);

In the following line, e contains the above created event-object with the assigned properties:

$('#txt-field').on('keydown', function (e) {

Demo

Side-Note: There might be some cross-browser issues when the keycodes are different in the browser.

If you only want to trigger the function onload:

$(document).ready(function(){ 
    scan();
});

Reference

jQuery - Event

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论