0

我完全准备好承认我在这里遗漏了一些简单的东西。我发现 jquery docs 页面上的文档并没有真正包含进行基本自定义验证所需的所有信息。

我的问题是我需要验证元素中不包含的数据,所以我一直在使用一种“虚拟”元素来尝试触发我的自定义方法。

<div id="modal-dialog-overlay">
<div id="modal-background"></div>
<article id="dialog-form" class="event-module module">
    <header>
        <h6>New Event</h6>
        <section class="module-actions">
            <span class="button" onclick="overlay_select('modal-dialog-overlay', true);">Cancel</span>
            <span class="primary button" onclick="createBEvent()">Create Event</span>
        </section>
    </header>
    <section class="wrapper">
        @using (Html.BeginForm("CreateEvent", "Conversations", FormMethod.Post, new { enctype = "multipart/form-data", id = "createEventForm", @class = "group" }))
        {
            <input id="participantsChecker" type="text" />
            <input class="submit" type="submit" value="Submit"/>
        }
    </section>
</article>
</div>
</div>

然后javascript位是

    $.validator.addMethod("checkParticipants", function (value, element) {
        alert($(".event-module .contact-handle [name='Key']").length);
        return $(".event-module .contact-handle [name='Key']").length
    }, "You need to invite at least one person.");
    $("#createEventForm").validate({
        rules: {
            participantsChecker: {
                checkParticipants: true
            }
        }
    });

这是在document.onready页面。页面上还有其他代码添加了选择器正在寻找的元素,这些元素肯定存在。在任何情况下,提交表单时都不会出现该警报。我只是通过点击提交而不在表单中输入任何内容来测试它。

4

1 回答 1

1

输入元素必须有一个名称,这就是 jQuery Validate 与它们交互的方式:

<input id="participantsChecker" name="participantsChecker" type="text" />

那应该行得通。

于 2012-06-11T23:12:50.590 回答