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 代码
基础教程推荐
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
