如何使用 JQuery 发送按键触发器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/807083/
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
How to send a keypress trigger using JQuery
提问by gath
How do i send a trigger keypress of a particular keyCode eg. "9" event on a TextBox using JQuery programmatically?
我如何发送特定 keyCode 的触发按键,例如。以编程方式使用 JQuery 的 TextBox 上的“9”事件?
This what i intend to do; Programmatically enter a value to a TextBox and then programmatically trigger a tabkey on the TextBox to exit the field.
这是我打算做的;以编程方式向 TextBox 输入一个值,然后以编程方式触发 TextBox 上的 tabkey 以退出该字段。
my code
我的代码
$("#tags1").val("comp") // Works well
$("#tags1").trigger("keypress",[9]); // Fails!!
Gath
加特
回答by millimoose
The jQuery documents state that it'll only pass normalized event attributes = the properties of the jQuery.Event object, which don't seem to include event-specific data.
jQuery 文档声明它只会传递规范化的事件属性 = jQuery.Event 对象的属性,这些属性似乎不包含特定于事件的数据。
I tried, and couldn't get it to insert text into a textbox even when reusing an event object that a browser keypress created, so it doesn't seem to be possible.
我尝试过,即使重用浏览器按键创建的事件对象,也无法将文本插入文本框,因此似乎不可能。
Why can't you just change the value of the textbox, and call blur() to exit it? (Or focus() on the one you want to enter next?)
为什么不能只改变文本框的值,然后调用 blur() 退出它?(或者 focus() 在你想输入的下一个?)
回答by system PAUSE
Not exactly what you asked, but instead of using keypress this might accomplish what you want:
不完全是你问的,但不是使用按键,这可能会完成你想要的:
$("#tags1").blur();
or even
甚至
$("#tags1").next().focus();
回答by TStamper
Try:
尝试:
$("#tags1").val("comp").trigger("keypress",[9]);
or
或者
$("#tags1").val("comp").trigger("keypress", [{
preventDefault:function(){},
keyCode:9
}]);
回答by Mark Lagendijk
I created a simple jQuery pluginwhich does solve this problem. It uses the ':tabbable' selector of jQuery UI to find the next 'tabbable' element and selects it.
我创建了一个简单的 jQuery 插件来解决这个问题。它使用 jQuery UI 的 ':tabbable' 选择器来查找下一个 'tabbable' 元素并选择它。
Example usage:
用法示例:
// Simulate tab key
$.tabNext();
回答by Erik Larsson
If you wan to add a value to an input / textarea then fire the keypress "enter" event.
如果您想向输入/文本区域添加一个值,则触发按键“输入”事件。
Trigger it like this
像这样触发
$('input').val( input_value ).blur();
回答by Kristoffer Bohmann
Try this:
尝试这个:
$('textarea:input').live('keypress', function(e) {
if (e.keyCode == 9) {
e.preventDefault();
// Press TAB to append a string (keeps the original TEXTAREA text).
$(this).append("TAB TAB TAB AFTER TEXTAREA TEXT");
// Press TAB to append a string (keeps the original TEXTAREA text).
$(this).focus().prepend("TAB TAB TAB BEFORE TEXTAREA TEXT");
// Press TAB to replace a all text inside TEXTAREA.
$(this).val("INSERT INTO TEXTAREA / REPLACE EXISTING TEXT");
}
});
If you want to add text insidethe textarea (by the active cursor - like CTRL+V), you might want to use "Tabby"(JS source code) (jQuery plugin - 254 code lines..!).
如果您要添加文本内的文字区域(由活动光标-像CTRL + V),您可能需要使用“虎斑” (JS代码)(jQuery插件- 254行代码..)。
You may also find inspiration in the YUI editorwhich will let you add TABs inside the editor.
您还可以在YUI 编辑器中找到灵感,它可以让您在编辑器中添加 TAB。