如何使用jQuery触发输入事件? [英] How to trigger an input event with jQuery?

查看:42
本文介绍了如何使用jQuery触发输入事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单击按钮时将'a'添加到输入值

I want to add 'a' to the value of input when click a button

这是我的代码(使用jQuery 1.4.4):

Here is my code(with jQuery 1.4.4):

$("#button").click(function(){
    $("#input").trigger("focus");
    var e = jQuery.Event("keypress");
    e.which = '97';
    $("#input").trigger(e);
})

但是,似乎只触发了焦点"事件,但未能按键".

However, it seems only to trigger 'focus' event ,but failed to 'keypress'.

推荐答案

像这样吗?抱歉,我对你的著作感到困惑.

like this?? Sorry, I'm confused with your writings..

$("#button").click(function(){
    $("#input").trigger("keypress") // you can trigger keypress like this if you need to..
    .val(function(i,val){return val + 'a';});
});

参考: .val(function(index,value));

这篇关于如何使用jQuery触发输入事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆