在使用JQuery显示警报后,防止关注下一个表单元素

时间:2022-08-23 22:40:12

I have some text inputs which I'm validating when a user tabs to the next one. I would like the focus to stay on a problematic input after showing an alert. I can't seem to nail down the correct syntax to have JQuery do this. Instead the following code shows the alert then focuses on the next text input. How can I prevent tabbing to the next element after showing an alert?

我有一些文本输入,当用户选中下一个时,我正在验证。在显示警报后,我希望重点关注有问题的输入。我似乎无法确定让JQuery执行此操作的正确语法。相反,以下代码显示警报,然后关注下一个文本输入。如何在显示警报后阻止标签到下一个元素?

$('input.IosOverrideTextBox').bind({
    blur: function(e) {
      var val = $(this).val();
      if (val.length == 0) return;
      var pval = parseTicks(val);
      if (isNaN(pval) || pval == 0.0) {            
        alert("Invalid override: " + val);
        return false;
      }
    },
    focus: function() {
      $(this).select();
    }
  });

2 个解决方案

#1


0  

I don't like forced focus, but can't you just focus after the blur takes place?

我不喜欢强制对焦,但是你不能只是在模糊发生后进行对焦吗?

element.focus();

If doing that in the blur event doesn't always work (I'm not sure exactly when it fires, before or after the actual blur takes place), a redundant timeout will do, as well: setTimeout(function () { element.focus() }, 0).

如果在模糊事件中这样做并不总是有效(我不确定它何时触发,在实际模糊发生之前或之后),冗余超时也将如此:setTimeout(function(){element。 focus()},0)。

But please don't do this. Heck, you should never be using alert or any kind of modal dialog for a web interface, either. How about adding a invalid class to the form field, putting a message off to the side of it, and disabling submit until all fields are valid? That's a much less invasive solution that allows me to fill out the form in whatever way is best for me, rather than whatever way is simplest for you.

但请不要这样做。哎呀,你永远不应该为web界面使用警报或任何类型的模态对话框。如何将无效的类添加到表单字段,将消息放在其旁边,并禁用提交直到所有字段都有效?这是一种侵入性较小的解决方案,它允许我以最适合我的方式填写表单,而不是以最简单的方式填写表单。

#2


0  

You can do this with the validation plugin by default.

默认情况下,您可以使用验证插件执行此操作。

focusInvalid default: true

focusInvalid默认值:true

Focus the last active or first invalid element on submit via validator.focusInvalid(). The last active element is the one that had focus when the form was submitted, avoiding to steal its focus. If there was no element focused, the first one in the form gets it, unless this option is turned off.

通过validator.focusInvalid()将最后一个活动元素或第一个无效元素聚焦在提交上。最后一个活动元素是在提交表单时具有焦点的元素,避免窃取其焦点。如果没有聚焦元素,则表单中的第一个元素会获得它,除非关闭此选项。

Then you'd only need to have the focus event handler do your select and let the plugin handle validation.

然后你只需要让焦点事件处理程序执行你的选择并让插件处理验证。

#1


0  

I don't like forced focus, but can't you just focus after the blur takes place?

我不喜欢强制对焦,但是你不能只是在模糊发生后进行对焦吗?

element.focus();

If doing that in the blur event doesn't always work (I'm not sure exactly when it fires, before or after the actual blur takes place), a redundant timeout will do, as well: setTimeout(function () { element.focus() }, 0).

如果在模糊事件中这样做并不总是有效(我不确定它何时触发,在实际模糊发生之前或之后),冗余超时也将如此:setTimeout(function(){element。 focus()},0)。

But please don't do this. Heck, you should never be using alert or any kind of modal dialog for a web interface, either. How about adding a invalid class to the form field, putting a message off to the side of it, and disabling submit until all fields are valid? That's a much less invasive solution that allows me to fill out the form in whatever way is best for me, rather than whatever way is simplest for you.

但请不要这样做。哎呀,你永远不应该为web界面使用警报或任何类型的模态对话框。如何将无效的类添加到表单字段,将消息放在其旁边,并禁用提交直到所有字段都有效?这是一种侵入性较小的解决方案,它允许我以最适合我的方式填写表单,而不是以最简单的方式填写表单。

#2


0  

You can do this with the validation plugin by default.

默认情况下,您可以使用验证插件执行此操作。

focusInvalid default: true

focusInvalid默认值:true

Focus the last active or first invalid element on submit via validator.focusInvalid(). The last active element is the one that had focus when the form was submitted, avoiding to steal its focus. If there was no element focused, the first one in the form gets it, unless this option is turned off.

通过validator.focusInvalid()将最后一个活动元素或第一个无效元素聚焦在提交上。最后一个活动元素是在提交表单时具有焦点的元素,避免窃取其焦点。如果没有聚焦元素,则表单中的第一个元素会获得它,除非关闭此选项。

Then you'd only need to have the focus event handler do your select and let the plugin handle validation.

然后你只需要让焦点事件处理程序执行你的选择并让插件处理验证。