我正在尝试使用表单方法checkValidity()。
http://html5test.com/告诉我,我的浏览器(Chrome)支持表单级别的checkValidity方法.
然而,使用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)};
});我收到一个错误:object has no method checkValidity()
我做错了什么?
发布于 2011-09-12 11:08:26
尝试:
$('#profileform')[0].checkValidity()选择$('#profileform')时,将得到一个jQuery对象数组。要访问实际的DOM属性,必须选择数组中的第一个项,即原始DOM元素。
发布于 2021-06-22 23:14:42
@robertc的答案是完美的。无论如何,我只想添加另一种方法,使用jQuery的.get([index])函数。它还检索给定索引的DOM元素,或者如果没有声明索引,则检索所有匹配的DOM元素。
最后,它是完全相同的,只是用一种更冗长的方式编写:
$('#profileform').get(0).checkValidity()把文档留在这里:https://api.jquery.com/get/
https://stackoverflow.com/questions/7386817
复制相似问题