如果单击提交按钮,则阻止执行 onblur 代码 [英] Prevent onblur code to execute if clicked on submit button

查看:48
本文介绍了如果单击提交按钮,则阻止执行 onblur 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过以下代码,我希望仅当 onblur 事件不是由于提交按钮上的ONCLICK"而引起时,才能在元素 id="eg1" 的ONBLUR"上做某事".

By the following code I wish to "DO SOMETHING" on "ONBLUR" of element id="eg1" only if the onblur event was not caused due to "ONCLICK" on the submit button.

    $(document).ready(function() {  
     $('#eg1').blur(function() {
      if(!($("#SubmitBut").click())) {
             //do something
      }
     });
    });

例如:如果用户更改eg1"文本字段的值并单击下一个文本字段,则必须运行 DO SOMETHING 代码,但如果用户更改eg1"字段的值然后单击在 SUBMIT 按钮上,则 DO SOMETHING 代码不得运行.

For Eg : if the user changes value of the "eg1" text field and clicks on the next text field then the DO SOMETHING code must run, but in case the user changes value of the the "eg1" field and then clicks on the SUBMIT button, then the DO SOMETHING code must not run.

这样做是否正确?请指导.

Is it the correct way to do so ?? Please guide.

推荐答案

blur 一个元素的事件在另一个元素的 click 事件之前触发.所以一种方法是使用 mousedownmouseup 事件来切换标志,因为一个元素的 mousedown 事件在 blur 另一个事件.

blur event of an element triggers before click event of another. So one way is to use mousedown and mouseup events to toggle a flag, because mousedown event of one element triggers before blur event of another one.

$("#eg1").on("blur", function(e){
  if($("#submit").data("mouseDown") != true){
      alert("DO SOMETHING");
  }
});

$("#submit").on("mousedown", function(e){
    $("#submit").data("mouseDown", true);
  });

$("#submit").on("mouseup", function(e){
    $("#submit").data("mouseDown", false);
  });

这篇关于如果单击提交按钮,则阻止执行 onblur 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆