New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

让光标定位到表单域会报错 #227

Closed
lileichinasoft opened this Issue Apr 4, 2016 · 9 comments

Comments

Projects
None yet
2 participants
@lileichinasoft
Copy link

lileichinasoft commented Apr 4, 2016

例如有个表单域是:
>input type="password" name="password" data-rule="帐号密码:required;password" placeholder="帐号密码">

加一段js:
document.getElementsByName("password")[0].focus();

直接报错:
Uncaught TypeError: Cannot read property 'fields' of undefined

去掉插件就没问题. 请问如何让光标定位到指定域?

@niceue

This comment has been minimized.

Copy link
Owner

niceue commented Apr 4, 2016

你应该使用jQuery的方法来获焦

$('input[name="password"]').focus();
@lileichinasoft

This comment has been minimized.

Copy link
Author

lileichinasoft commented Apr 4, 2016

用jquery 焦点是可以获取的, 还是会报同样的错误
Uncaught TypeError: Cannot read property 'fields' of undefined

@niceue

This comment has been minimized.

Copy link
Owner

niceue commented Apr 4, 2016

@lileichinasoft nice-validator 版本是

@lileichinasoft

This comment has been minimized.

Copy link
Author

lileichinasoft commented Apr 4, 2016

0.10.8

@niceue

This comment has been minimized.

Copy link
Owner

niceue commented Apr 4, 2016

@lileichinasoft 能提供例子重现问题吗?

@lileichinasoft

This comment has been minimized.

Copy link
Author

lileichinasoft commented Apr 4, 2016

简单描述一下啊,markdown排版不很熟悉,抱歉, 这里只是简单描述一下,js脚本没写到script标签里,
假如有个表单,里面有个文本域, 在js里设置焦点会报错:Uncaught TypeError: Cannot read property 'fields' of undefined

<form class="sso-form" id="acComfirmForm" method="post" action=""> <input type="text" name="password" value=""/> </form>
$('input[name="password"]').focus();

@niceue

This comment has been minimized.

Copy link
Owner

niceue commented Apr 4, 2016

@lileichinasoft 单纯是你这段代码是不会报错的。

@lileichinasoft

This comment has been minimized.

Copy link
Author

lileichinasoft commented Apr 6, 2016

@niceue 上面的代码, 引入nice-validator就会报错,很奇怪啊

@niceue niceue added the tracking label May 8, 2016

@niceue

This comment has been minimized.

Copy link
Owner

niceue commented May 10, 2016

找到问题了,因为你的这句代码没有在 DOM ready 事件后执行。

你的代码$('input[name="password"]').focus() 在触发 focus 的时候会执行初始化表单验证,但是这时候表单还没准备好。如果你这样写就不会有问题:

$(function(){
    $('input[name="password"]').focus()
});

插件会在 1.0 版本屏蔽这个报错

@niceue niceue added bug and removed tracking labels May 10, 2016

@niceue niceue added this to the 1.0.0 milestone May 10, 2016

@niceue niceue added improve and removed bug labels May 10, 2016

@niceue niceue closed this in f7f81e2 May 10, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment