如何重点关注首次输入? [英] How to focus first input?

查看:69
本文介绍了如何重点关注首次输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC应用程序,我想关注错误的第一个字段.提交时,如果最后一个字段有错误,并且用户尝试提交表单,则焦点将转到错误的最后一个字段,而不是第一个字段.我尝试了多个,包括以下内容:

I have a ASP.NET MVC application and I want to focus the first field in error. On submit, if the last field has error and user tries to submit the form, the focus is going to the last field in error instead of the first field. I tried multiple including the below:

$().ready(function() {
    $("#Form").submit(function() {
        $('.input-validation-error').focus();
        $(".input-validation-error").each(function() {
            $(this).focus();
        });

    });
}); 

有什么解决方案可以使用户始终可以错误地转到第一个字段.

Is there any solution where the user can always go to the first field in error.

推荐答案

无需在此处使用每个方法并遍历所有错误.您可以将焦点设置为错误类别为input-validation-errorfirst元素,例如:

No need to use each here and loop over all errors. You can just set the focus to first element with error class input-validation-error like:

$('.input-validation-error:first').focus();

这篇关于如何重点关注首次输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆