Prevent onblur code to execute if clicked on submit button(如果单击提交按钮,则阻止执行 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
事件之前触发.所以一种方法是使用 mousedown
和 mouseup
事件来切换标志,因为一个元素的 mousedown
事件在 blur之前触发code> 另一个事件.
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 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果单击提交按钮,则阻止执行 onblur 代码


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Fetch API 如何获取响应体? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07