如何在不输入的情况下使用 jquery 启动输入文本框按键事件
How fire up input textbox keypress event using with jquery without typing
我想在不打字的情况下使用 jquery 启动输入文本框 keypress
事件。
看看这个片段:
var e = jQuery.Event("keypress");
e.keyCode = $.ui.keyCode.ENTER;
$("input").trigger(e);
这是我想要的吗?
但是这些代码有错误:
Cannot read property 'which' of undefiend
我如何绕过该错误并在不输入的情况下使用 jquery 启动输入文本框 keypress
事件?
我也试过这个:
$('#input').val('string');
$('#input').keypress();
同样的错误
要使 $.ui.keyCode
正常工作,您需要包含 jquery-ui.js
没有jquery-ui
$(function(){
var e = $.Event("keypress");
e.which = 13;
$('input').trigger(e);
});
$('input').on('keypress',function(e){
if(e.which==13)
alert('Enter pressed automatically');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
和jquery-ui
$(function(){
var e = $.Event("keypress");
e.which = $.ui.keyCode.ENTER;
$('input').trigger(e);
});
$('input').on('keypress',function(e){
if(e.which==13)
alert('Enter pressed automatically');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<input type="text" />
将此解决方案与 jquery
一起使用
$(document).ready(function() {
var e = $.Event("keypress", {which: 13});
$('input').trigger(e);
});
$('input').on('keypress',function(e){
console.log(e.which+' -- KeyPress event Fired');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
我想在不打字的情况下使用 jquery 启动输入文本框 keypress
事件。
看看这个片段:
var e = jQuery.Event("keypress");
e.keyCode = $.ui.keyCode.ENTER;
$("input").trigger(e);
这是我想要的吗?
但是这些代码有错误:
Cannot read property 'which' of undefiend
我如何绕过该错误并在不输入的情况下使用 jquery 启动输入文本框 keypress
事件?
我也试过这个:
$('#input').val('string');
$('#input').keypress();
同样的错误
要使 $.ui.keyCode
正常工作,您需要包含 jquery-ui.js
没有jquery-ui
$(function(){
var e = $.Event("keypress");
e.which = 13;
$('input').trigger(e);
});
$('input').on('keypress',function(e){
if(e.which==13)
alert('Enter pressed automatically');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />
和jquery-ui
$(function(){
var e = $.Event("keypress");
e.which = $.ui.keyCode.ENTER;
$('input').trigger(e);
});
$('input').on('keypress',function(e){
if(e.which==13)
alert('Enter pressed automatically');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<input type="text" />
将此解决方案与 jquery
一起使用$(document).ready(function() {
var e = $.Event("keypress", {which: 13});
$('input').trigger(e);
});
$('input').on('keypress',function(e){
console.log(e.which+' -- KeyPress event Fired');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" />