-2
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
        <script>
        $(document).ready(function () {
            $("[mandatory]").rules("add", {required: true});
            $("form").validate();
        })
        </script>
    </head>
    <blink>
        <body>
            <form action="#">
                <fieldset>
                    <legend>A simple comment form with submit validation and default messages</legend>
                    <p>
                        <label for="cname">Name</label><em>*</em>
                        <input id="cname" name="name" size="25" mandatory="true"/>
                    </p>
                    <p>
                        <label for="cemail">E-Mail</label> <em>*</em>
                        <input id="cemail" name="email"  size="25" mandatory="true" />
                    </p>
                    <p>
                        <label for="ccomment">Your comment</label> <em>*</em>
                        <textarea id="ccomment" name="comment" cols="22" ></textarea>
                    </p>
                    <p>
                        <input class="submit" type="submit" value="Submit" />
                    </p>
                </fieldset>
            </form>
        </body>
    </blink>
</html>

它不起作用,它在这条线上说:

var settings = $.data(element.form, 'validator').settings;

** Microsoft JScript 运行时错误:无法获取属性“设置”的值:对象为空或未定义**

4

1 回答 1

1

你为什么不像文档中提到的那样添加arequired作为所有强制方法的类?

您可以手动添加它们,或者

$("[mandatory='true']").addClass("required");

数据属性:

mandatory 不是 HTML 世界中的有效属性,所以我强烈建议您使用该data属性并将您的更改mandatorydata-mandatory,使其符合 HTML 规范。

为此,您的电话将更改为:

$("[data-mandatory='true']").addClass("required");

...但我仍然不明白为什么不使用required类名,如果需要,只需添加它,如果不需要,不要添加它。


关于您的settings 错误,请停止使用演示 URL 并使用插件页面中的 CDN 链接:

http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js

这是一个

活生生的例子


PS <blink> 不是标准的 HTML 元素

于 2012-07-04T14:40:52.647 回答