Prevent onblur code to execute if clicked on submit button

前端 未结 3 1466
长情又很酷
长情又很酷 2021-02-04 12:15

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.

<         


        
3条回答
  •  滥情空心
    2021-02-04 12:25

    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);
      });
    

提交回复
热议问题