简体   繁体   English

在字段旁边设置按钮焦点,以便它使用jQuery的Enter或Mouse单击

[英]Set button focus next to field so that it uses either Enter or Mouse click with jQuery

I want to be able to either Press the enter key or click the mouse to submit the info in the inputbox. 我希望能够按Enter键或单击鼠标以在输入框中提交信息。

Right now when you manually change focus using tab or click with mouse it will submit the info. 现在,当您使用Tab键手动更改焦点或使用鼠标单击时,它将提交信息。 It is using the jQuery live Click method. 它使用的是jQuery live Click方法。 I found this solution for setting focus to a button 我找到了将焦点设置为按钮的解决方案

Setting focus to a button next to a text box 将焦点设置为文本框旁边的按钮

but I don't know how to implement that so I can listen using the live click and do either or. 但我不知道该如何实现,因此我可以使用实时点击来收听并执行“或”操作。 A mouse click or the enter button. 鼠标单击或输入按钮。 Any help is appreciated. 任何帮助表示赞赏。

example: be able to do this using either the enter button with focus or a mouse click. 示例:可以使用具有焦点的Enter按钮或单击鼠标来执行此操作。

$('#theinput').keypress(function(event) { 
  if (event.keyCode == '13') { 
    $('#mybutton').click(); 
    event.preventDefault(); 
  } 
}); 

Problem is I don't know how to convert that example to also use something like this 问题是我不知道如何将示例转换为也使用类似的内容

$('a.button').live('click',function(){
//do stuff here
)};

I would do this using a named function: 我可以使用命名函数来做到这一点:

$(document).ready(function(){
    var submitter = function() {
       // do something
    };

    $('#theinput').keypress(function(event) {
        if (event.keyCode == '13') {
            event.preventDefault(); 
            submitter();
        } 
    });

    $('a.button').live('click', submitter);
});

This uses the same function for both events without triggering another unnecessary event, which would cause performance issues. 这对两个事件使用相同的功能,而不会触发另一个不必要的事件,这将导致性能问题。

I think if you change this: 我认为如果您更改此设置:

$('a.button').live(click,function(){

to this: 对此:

$('a.button').live("click",function(){

it should work 它应该工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM