我有很少的输入字段需要更新。当按Tab键时,我需要将焦点移动到下一个字段,只有在对当前字段进行一些验证成功之后。如果失败,则保持在同一个字段中。防止在Firefox中默认的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哈桑的代码----
<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中工作正常。
要触发哪个事件的fieldFocus函数?是否可以用你的代码创建jsFiddle来触发该函数? – 2013-03-05 11:13:52
@Faizul哈桑,我已经更新了代码示例..对不起,我不能更新它到jsFiddle。希望你能理解我的要求.. – idsTech 2013-03-05 11:36:46
我不认为你必须通过下一个领域。只需尝试我已发布的答案。它工作正常...希望这会帮助你.. – 2013-03-05 11:41:19