jQuery:我需要 keyUp 和 change 事件吗?

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

jQuery: Do I need both keyUp and change events?

javascriptjquery

提问by Ionut Flavius Pogacian

I have a page with 2 forms; each form has an input text field;

我有一个包含 2 个表单的页面;每个表单都有一个输入文本字段;

The value found in the input text of the 1st form must be the same with the value found in the input text of the 2nd form;

在第一种形式的输入文本中找到的值必须与在第二种形式的输入文本中找到的值相同;

When the value chages, the other input text field also modifies its value;

当值发生变化时,其他输入文本字段也会修改其值;

Should I use both onkeyupand onchangeevents?

我应该同时使用onkeyuponchange事件吗?

If not, wich one is the right one to use?

如果没有,哪一种是正确使用的?

I am asking this because it seams that I have 2 post requests, and i think that this is why.

我问这个是因为我有 2 个帖子请求,我认为这就是原因。

   $('input.searchbox').keyup( function(){
       $('input.txtfieldsearch').val($('input.searchbox').val());
       listViewUpdate();
       $('input.txtfieldsearch').trigger("keyup");
   });

    $('input.txtfieldsearch').keyup( function(){
       $('input.searchbox').val($('input.txtfieldsearch').val());
       listViewUpdate();
    });

   $('input.searchbox').change( function(){
       $('input.txtfieldsearch').val($('input.searchbox').val());
       listViewUpdate();
       $('input.txtfieldsearch').trigger("change");
   });

    $('input.txtfieldsearch').change( function(){
       $('input.searchbox').val($('input.txtfieldsearch').val());
       listViewUpdate();
    });

回答by rigobcastro

You can use $.on()function and attach multiples handlers.

您可以使用$.on()函数并附加多个处理程序

$("#element").on('keyup change', function (){
   // Your stuff...
});

回答by SeinopSys

onkeyupmostly fires when a key was pressed by the user and then released. onchangecan happen if the user uses the browser's autocomplete feature. You should use the .on()method to catch both events, just in case:

onkeyup主要是在用户按下某个键然后松开时触发。onchange如果用户使用浏览器的自动完成功能,就会发生这种情况。您应该使用该.on()方法来捕获这两个事件,以防万一:

$('input.searchbox').on('keyup change', function(e){
    $('input.txtfieldsearch').val($('input.searchbox').val());
    listViewUpdate();
    $('input.txtfieldsearch').trigger(e.type);
});

$('input.txtfieldsearch').on('keyup change', function(){
    $('input.searchbox').val($('input.txtfieldsearch').val());
    listViewUpdate();
});

回答by Swarup Sengupta

You can use only "input" event if you don't need to provide support for the older browsers. It will be fired everytime a value changes in the input element. Here's a list of the supported browsers: https://developer.mozilla.org/en-US/docs/Web/Events/input

如果您不需要为旧浏览器提供支持,则只能使用“输入”事件。每次输入元素中的值更改时都会触发它。以下是支持的浏览器列表:https: //developer.mozilla.org/en-US/docs/Web/Events/input

回答by ColBeseder

If you want to react to change, you should use change.

如果你想对变化做出反应,你应该使用change.

There are some occasions where a keyup doesn't make a change(like shift) and occasions where there's a change without a keyup (paste).

在某些情况下,keyup 不会进行更改(例如 shift),而有时会在没有 keyup 的情况下进行更改(粘贴)。