Safari 中的 jQuery 键盘输入和“输入”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3850212/
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-26 16:13:39  来源:igfitidea点击:

jQuery keyup and 'enter' in Safari

jquerysafarionkeyup

提问by Martin

I am using jQuery and want to make it possible for the user to hit enter after entering data into a search field to start the search.

我正在使用 jQuery 并希望让用户在将数据输入到搜索字段后按 Enter 键开始搜索。

I'm using the following code:

我正在使用以下代码:

        $('#textSearch').keyup(function (event) {
            if (event.keyCode == '13') {
                doSearch();
            }
            return false;
        });

It works perfect in Firefox and IE but not at all in Safari. What happens is that the form is being submitted when I hit enter in safari and that is not what I want.

它在 Firefox 和 IE 中完美运行,但在 Safari 中则完全不可用。发生的情况是,当我在 safari 中按 Enter 键时正在提交表单,这不是我想要的。

Adding onsubmit="return false;" to the form works but is not an option as the form tag is on a masterpage of an asp.net page and I need the form to be submitted on other pages.

添加 onsubmit="return false;" 表单有效但不是一个选项,因为表单标签位于 asp.net 页面的母版页上,我需要在其他页面上提交表单。

Is there a way to also get this feature working in Safari?

有没有办法让这个功能在 Safari 中工作?

EDIT: I also tried to just show an alert instead of the doSearch() function. The alert shows up fine but after that the form is being submitted.

编辑:我也尝试只显示警报而不是 doSearch() 函数。警报显示正常,但之后正在提交表单。

采纳答案by meouw

Browsers may vary as to which event triggers a submit. In this case Safari may be submitting on the keydown event.
You could watch for the submit event without changing the markup and cancel it:

浏览器可能会因触发提交的事件而异。在这种情况下,Safari 可能会提交 keydown 事件。
您可以在不更改标记的情况下监视提交事件并将其取消:

$('#the-id-of-the-form').submit( function( e ) {
    e.preventDefault();
});

You can then listen for the keyup event and handle it as you are doing now.

然后,您可以监听 keyup 事件并像现在一样处理它。

回答by Sarfraz

Try this:

尝试这个:

    $('#textSearch').keyup(function (event) {
        var key = event.keyCode || event.which;

        if (key === 13) {
            doSearch();
        }
        return false;
    });

回答by Praveen Prasad

add prevent deault

添加阻止默认值

$('#textSearch').keyup(function (event) {
    if (event.keyCode == '13') {
        event.preventDefault();
        doSearch();
    }
    return false;
});

回答by user472059

I use "keydown" because "keyup" didn't work for me. I also needed for this enter to be disabled with input fields that I am adding with AJAX, thus the "live".

我使用“keydown”是因为“keyup”对我不起作用。我还需要使用我使用 AJAX 添加的输入字段禁用此输入,因此是“实时”。

You'll need to bind a "change" handler to the field. This version exits the field ("blur") then triggers a change on the field, then fires the change. Without the blur, the change is triggered twice!

您需要将“更改”处理程序绑定到该字段。此版本退出该字段(“模糊”),然后触发该字段的更改,然后触发该更改。没有模糊,变化被触发两次!

 $('#textSearch').live('keydown', function(event) {
  if(event.keyCode == 13)
  {
  $(event.target).blur();
  $(event.target).change();
  event.preventDefault();
  return false;
  }
 }
);

回答by user472059

I use "keydown" because "keyup" didn't work for me. I also needed for this enter to be disabled with input fields that I am adding with AJAX, thus the "live".

我使用“keydown”是因为“keyup”对我不起作用。我还需要使用我使用 AJAX 添加的输入字段禁用此输入,因此是“实时”。

You'll need to bind a "change" handler to the field. This version exits the field ("blur") then triggers a change on the field, then fires the change. Without the blur, the change is triggered twice!

您需要将“更改”处理程序绑定到该字段。此版本退出该字段(“模糊”),然后触发该字段的更改,然后触发该更改。没有模糊,变化被触发两次!

    $('#textSearch').live('keydown', function(event) {
  if(event.keyCode == 13)
  {
  $(event.target).blur();
  $(event.target).change();
  event.preventDefault();
  return false;
  }
 });