jQuery 我可以防止发生模糊事件吗?

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

Can i prevent blur event from happening?

jqueryevents

提问by Tomer

I have a simple example where i have an inputfield and i put a blurand changeevent on it:

我有一个简单的例子,我有一个input字段,我在上面放了一个blurchange事件:

HTML

HTML

<input name="test" value=""/>

JS

JS

$('input').change(function(e){

 alert('change'); 

});

$('input').blur(function(e){

 alert('blur'); 
});

Is it possible to prevent the blurevent from happening if the changeevent was triggered?

blur如果change事件被触发,是否可以阻止事件发生?

One way would be to define a boolean that changes when the change event was triggered, but I don't like it, is there a better way?

一种方法是定义一个在触发更改事件时更改的布尔值,但我不喜欢它,有没有更好的方法?

Hereis an example you can play with.

是您可以使用的示例。

回答by Akhil

Solved issue by this..

解决了这个问题..

 $('input').change(function(e){
  alert('change'); 
   e.stopImmediatePropagation();
   $(this).off("blur");
 });

$('input').focus(function(e){
   $(this).on("blur", function(e){
   e.stopImmediatePropagation();
   e.preventDefault();
  alert('blur'); });
});

回答by Pragnesh Chauhan

use .off

.off

DEMO

演示

$('input').change(function(e){
  alert('change'); 
  e.stopImmediatePropagation();
  $(this).off("blur"); //$("input[name='test']").off("blur");
});

$('input').blur(function(e){  
  e.preventDefault();
 alert('blur'); 
});?

回答by Raab

you can use

您可以使用

this.unbind('blur');on the onchange call

this.unbind('blur');在通话中

回答by s.Daniel

If I understand you correctly the answer is to use event.preventDefault();See http://jsbin.com/welcome/52201/editfor a demo.

如果我理解正确,答案是使用event.preventDefault();See http://jsbin.com/welcome/52201/edit进行演示。