我如何判断焦点事件是由于输入按下=表单提交而发生还是仅仅因为单击而发生?进入控制台的事件数据是“focusout”类型,没有相关信息
$(".clientrow[clientid="+clientid+"] td."+fieldname+"").bind("focusout", function(event){
console.log(event);
setTimeout(function() {
if (!event.delegateTarget.contains(document.activeElement)) {
$(".clientrow[clientid="+clientid+"] td."+fieldname+"").html(
$(".clientrow[clientid="+clientid+"] td."+fieldname+" input[type=text]").val()
);
}
}, 0);
});
最佳答案
编辑:正如 Oriol 在评论中指出的那样,这在 Mozilla 中不起作用。如果您只寻找 webkit 浏览器,您可以尝试这种方法。但作为通用解决方案,尝试在提交按钮上绑定(bind)一个事件,并连续设置一个标识该元素的标志。根据该元素,您可以检测它是否是真正的模糊。
您可以在事件
中查找latedTarget
属性。
演示:http://jsfiddle.net/GCu2D/782/
JS:
$(document).ready(function () {
$("input").on("blur", function (e) {
console.log(e);
if (e.relatedTarget) {
console.log("Because of button");
} else {
console.log("Just a blur")
}
});
});
HTML:
<form>
<input type="text" />
<button type="button">Submit</button>
</form>
您必须将 focusout
替换为 blur
事件。当模糊是由于单击按钮而导致时, relatedTarget 属性将使用 button
作为值,但在其他情况下它将为 null。
关于javascript - jquery 聚焦 == 提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31490917/