我正在使用来自bassistance.de的jQuery验证plugin。它工作正常。
来自<head>
:
<script type="text/javascript" src="/static/JQuery.js"></script>
<script type="text/javascript" src="/static/js-lib/jquery.validate.pack.js"></script>
<script type="text/javascript" src="/static/js-lib/jquery.validate.additional-methods.js"></script>
起初,这是我唯一的验证码,它起作用了:
$("form").validate();
$("#form-username").rules("add", {
required: true,
email: true,
});
验证了这个HTML:
<form id="form-username-form" action="api/user_of_email" method="get">
<p>
<label for="form-username">Email:</label>
<input type="text" name="email" id="form-username" />
<input type="submit" value="Submit" id="form-submit" />
</p>
</form>
太棒了,一切正常。但后来我添加了这个JS:
$("#form-choose-options input[type='text']").rules("add", {
number: true,
});
验证此标记:
<form id="form-choose-options" action="api/set_options" method="get">
<p>
<label for="form-min-credits">Min credits per term:</label><input type="text" name="min_credits" id="form-min-credits" /> <br />
<label for="form-optimal-credits">Optimal credits per term:</label><input type="text" name="optimal_credits" id="form-optimal-credits" /> <br />
<label for="form-max-credits">Max credits per term:</label><input type="text" name="max_credits" id="form-max-credits" /> <br />
<label for="form-low-GPA">Lowest acceptable GPA:</label><input type="text" name="low_GPA" id="form-low-GPA" /> <br />
<label for="form-high-GPA">Highest realistic GPA:</label><input type="text" name="high_GPA" id="form-high-GPA" /> <br />
<input type="hidden" class="user-pk" name="pk"/>
<input type="submit" value="Submit" />
</p>
</form>
这会导致文档加载时出现javascript错误:
$.data(f.form, "validator") is undefined
错误来自packer
函数。
我做错了什么?
答案 0 :(得分:1)
$("form").validate();
$("#form-username").rules("add", {
required: true,
email: true,
});
您的对象中有逗号逗号。消除对象定义中的最后一个逗号,一切都应该正常工作。
$("form").validate();
$("#form-username").rules("add", {
required: true,
email: true //Comma removed
});
你的另一个对象......
$("#form-choose-options input[type='text']").rules("add", {
number: true //Comma removed
});