Html 未找到 html5 表单 checkValidity() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7386817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:34:36  来源:igfitidea点击:

html5 form checkValidity() method not found

htmlformscheckvalidity

提问by Journeyman

I am trying to use the form method checkValidity().

我正在尝试使用表单方法 checkValidity()。

http://html5test.com/tells me that my browser (Chrome) support the form-level checkValidity method.

http://html5test.com/告诉我我的浏览器 (Chrome) 支持表单级别的 checkValidity 方法。

However, using jsfiddle http://jsfiddle.net/LcgnQ/2/I have tried the following html and javascript snippets:

但是,使用 jsfiddle http://jsfiddle.net/LcgnQ/2/我尝试了以下 html 和 javascript 片段:

<form id="profileform" name="profileform">
    <input type="text" id="firstname" required>
    <input type="button" id="testbutton" value="Test">
</form>

$('#testbutton').bind('click',function(){

    try{
    alert($('#profileform').checkValidity());
    }
    catch(err){alert('err='+err)};
});

I'm getting an error: object has no method checkValidity()

我收到一个错误: object has no method checkValidity()

What am I doing wrong?

我究竟做错了什么?

Thanks.

谢谢。

回答by robertc

Try:

尝试:

$('#profileform')[0].checkValidity()

When you select $('#profileform')you get a jQuery object array. To access actual DOM properties you must select the first item in the array, which is the raw DOM element.

当您选择时,$('#profileform')您将获得一个 jQuery 对象数组。要访问实际的 DOM 属性,您必须选择数组中的第一项,即原始 DOM 元素。