我有几个字段需要在 onblur 之后检查最小值,如果值不正确则返回到该字段。 $(this).focus() 似乎不起作用。
$(".PartMin").blur(function()
{
var Value = this.value.replace("$", "");
if(Value < 115)
{
alert("Value cannot be less than $115.00");
$(this).focus();
}
});
最佳答案
在模糊回调中设置焦点通常会出现问题。我发现引入延迟会有所帮助:
$(".PartMin").blur(function()
{
var Value = this.value.replace("$", "");
if(Value < 115)
{
alert("Value cannot be less than $115.00");
var element = this;
setTimeout(function() {
$(element).focus();
}, 50);
}
});
Live Example (由于代码片段 UI,不适用于堆栈代码片段)。
<小时/>但是:我强烈建议不要对模糊进行侵入式验证(弹出警报)。让用户在字段中移动,并提供非侵入性反馈(颜色等),并且仅在用户尝试使用数据确认操作时提供侵入性反馈。
关于javascript - 使用 "this"onblur 后 jQuery 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50735351/