【发布时间】:2013-03-05 11:01:43
【问题描述】:
我几乎没有要更新的输入字段。当按 Tab 键时,只有在当前字段的某些验证成功后,我才需要将焦点移动到下一个字段。如果失败,则留在同一字段中。
function fieldFocus(e, nxFld){
var key;
if (window.event) key = e.keyCode;
else if (e.which) key = e.which;
if (!e.shiftKey && key === 9) {
e.stopPropagation();
e.preventDefault();
// do validate {}
if (success)
$(nxFld).focus(); //set the focus to the next fld
else
// remain in the same field
}
return false;
}
$(currFld).bind("keydown",function(e) {
return fieldFocus(e, nxtFld);
});
这在 IE 和 Chrome 中运行良好。但在 Firefox 中,默认焦点总是在验证之前触发。请帮助我防止Firefox的默认行为。
---- 与@Faizul Hasan 的代码相关的编辑代码----
<script>
function fieldFocus(e, obj){
var key;
if (window.event) key = e.keyCode;
else if (e.which) key = e.which;
if (!e.shiftKey && key === 9) {
// do validate
if (0 !== obj.value.length){
var answer = confirm('Are you sure?')
if(answer)
return true;
else{
// need to stop cursor focus to the next field
e.stopPropagation();
e.preventDefault();
}
}
else{
e.stopPropagation();
e.preventDefault();
}
}
return false;
}
</script>
这是我真正遇到问题的地方,在用户确认焦点移动到 Firefox 中的下一个字段之前。但在 IE 和 Chrome 中它工作正常。
【问题讨论】:
-
你没有尝试阻止默认然后运行验证然后手动关注下一个字段吗?
-
TO哪个事件触发了fieldFocus函数??是否可以使用您的代码创建 jsFiddle 您触发该功能的事件?
-
@Faizul Hasan,我已经更新了代码示例。抱歉,我无法将其更新到 jsFiddle。希望你能理解我的要求..
-
我认为您不必通过下一个字段。只需尝试我发布的答案即可。它工作正常...希望对您有所帮助..
标签: javascript jquery firefox focus