Javascript 如何使用 jQuery 在输入字段中模拟输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13944835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:12:39  来源:igfitidea点击:

How to simulate typing in input field using jQuery?

javascriptjquerygreasemonkeykeyboard-eventstextinput

提问by user1422652

What I want is to simulate typing in <input>field using javascript.

我想要的是<input>使用 javascript模拟在字段中输入。

I have the following code:

我有以下代码:

var press = jQuery.Event("keydown");
press.ctrlKey = false;
press.which = 65;
$("#test").trigger(press);

But when I load the page, the #testinput field has no typed characters, the keycode of '65' represents 'A', but there is no 'A' input.

但是当我加载页面时,#test输入字段没有输入字符,'65'的键码代表'A',但没有'A'输入。

Basically what I want is to automatically typing in the website using Greasemonkey.

基本上我想要的是使用 Greasemonkey 自动输入网站。

Please give me some ideas or some library with which I can use to do this.
Thanks a lot!

请给我一些想法或一些图书馆,我可以用它来做到这一点。
非常感谢!

回答by Brock Adams

You can send key events, and anything listening for them will get them, but they will not change the input, so you will not see the letter Aappear, for example. This is mostly a security thing; see "Manually firing events"for a discussion about that.

你可以发送按键事件,任何监听它们的东西都会得到它们,但它们不会改变 input,所以你不会看到字母A出现,例如。这主要是一个安全问题;有关此问题的讨论,请参阅“手动触发事件”

So, if you want the letter to appear, you must alter the input's value as you send the key event. There is a jQuery plugin for that, see "The $.fn.sendkeysPlugin".

因此,如果您希望字母出现,则必须在发送键事件时更改输入的值。有一个 jQuery 插件,请参阅$.fn.sendkeys插件”

You can see how an <input>reacts with user-applied keys, key events, and that plugin at this jsFiddle.

您可以<input>此 jsFiddle 中查看 a对用户应用的键、键事件和该插件的反应

For reference, this is the key piece of code from that jsFiddle:

作为参考,这是来自 jsFiddle 的关键代码段:

$("button").click ( function (zEvent) {
    if (zEvent.target.id == "simA_plain") {
        console.log ("Send Plain key event");
        var keyVal = 65;
        $("#eventTarg").trigger ( {
            type: 'keypress', keyCode: keyVal, which: keyVal, charCode: keyVal
        } );
    }
    else {
        console.log ("Use the Plugin to simulate a keystroke");
        $("#eventTarg").sendkeys ("B") ;
    }
} );




That plugin should be sufficient if you are just trying to "simulate typing on an <input>". However, depending on what you are reallytrying to do, you may need to do one or more of the following:

如果您只是想“模拟输入”,那么该插件就足够了<input>。但是,根据您真正想做的事情,您可能需要执行以下一项或多项操作:

  1. Just set the text to what you want it to be.
  2. Send a keydownevent, if the page's javascript triggers off of that.
  3. Likewise, send a changeevent, etc., if the page's javascript triggers off of that.
  4. Just find and call the page's javascript directly. Use script injection, the location hack, unsafeWindow, and/or @grant nonemode to do that.
  5. Something else? State your true objective and link to the target page.
  1. 只需将文本设置为您想要的内容即可。
  2. 发送一个keydown事件,如果页面的 javascript 触发了该事件。
  3. 同样,change如果页面的 javascript 触发了该事件,则发送事件等。
  4. 只需找到并直接调用页面的 javascript。使用脚本注入,该位置黑客unsafeWindow和/或@grant none模式来做到这一点。
  5. 还有什么?陈述您的真实目标并链接到目标页面。