“此[0]未定义".使用jQuery validate插件的消息 [英] "this[0] is undefined" message using jQuery validate plugin
问题描述
我已经开始使用jQuery validate插件.我在显示错误消息时遇到了一些问题,并想创建一个测试页,可以在其中进行一些尝试.尽管昨天同样的设置对我有用,但我现在收到以下消息:
I've started to use the jQuery validate plugin. I was having a few issues with the display of the error messages and wanted to create a test page where I could experiment with a few things. Despite the same setup working for me yesterday, I'm now getting the follwing message:
this[0] is undefined
查看jQuery代码,它在以下部分(突出显示的特定行)中失败:
looking at the jQuery code, it fails in the following section (specific line highlighted):
valid: function() {
if ( $(this[0]).is('form')) {
return this.validate().form();
} else {
var valid = true;
**var validator = $(this[0].form).validate();**
this.each(function() {
valid &= validator.element(this);
});
return valid;
}
}
从查看的角度来看,它必须认为我的验证者不是表格,但确实如此.不太了解发生了什么.仅当尝试将 valid()方法的结果打印到控制台时,代码才会失败.到目前为止,这是我的代码.感谢您的帮助.
from looking at it, it must think that my validator isn't a form, but it is. Don't really understand what's going on. The code only fails when try to print out the result of the valid() method to the console. Here's my code so far. Grateful for any help.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
th {
text-align: center;
}
.labelCol {
width: 60px;
}
</style>
<script type="text/javascript" src="jquery-1.6.1.full.js"></script>
<script type="text/javascript" src="jquery.validate.full.js"></script>
<script type="text/javascript">
$('#myform').validate({
rules: {
thisval: "required"
}
});
console.info($('#myform').valid());
</script>
</head>
<body>
<form id="myform" name="myform" action="" method="post">
<table>
<tr>
<th colspan="2">Header Section</th>
</tr>
<tr>
<td class="labelCol">This Title</td>
<td class="valueCol">
<input type="text" name="thisval" id="thisval"
maxlength="45" size="45" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
推荐答案
执行脚本时不会加载您的#myform
表单,因此$('#myform')
不匹配任何内容.将脚本包装在就绪的事件处理程序中.
Your #myform
form is not loaded when the script is executed, so $('#myform')
doesn't match anything. Wrap your script in a ready event handler.
<script type="text/javascript">
$(function() { // <-- add this
$('#myform').validate({
rules: {
thisval: "required"
}
});
console.info($('#myform').valid());
});
</script>
这篇关于“此[0]未定义".使用jQuery validate插件的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!