jquery.validation 需要不停止回发

发布于 2024-12-26 19:18:35 字数 236 浏览 4 评论 0原文

我正在使用 jQuery 验证来尝试阻止用户回发,除非他满足几个条件。但所需的选项似乎并没有阻止表单提交...

这是 jsFiddle 的示例: jsFiddle

我仔细检查了我的 jQuery 代码,看起来好像它正在工作。 jsFiddle 说它是有效的代码。所以我不确定挂断是什么?

谢谢

I am using jQuery validation to attempt to keep a user from posting back unless he meets a couple of qualifications. But the required option doesn't seem to be keeping the form from submitting...

Here is jsFiddle with an example: jsFiddle

I double checked my jQuery code and it would seem as though it was working. jsFiddle says it valid code. So I am not sure what the hangup is?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-01-02 19:18:35

jQuery.validate 使用规则名称,而不是 id。因此,请将输入字段中的 id 替换为名称。

此外,您的表单应为

而不是

这是您的固定代码的链接:

<form id="temp1">
    <input type="text" name="HospitalFinNumber"/> Hospital Fin<br/>
    <input type="text" name="DateOfBirth"/>Date of Birth<br/>
    <input type="text" name="AdmitDate"/>Admit Date<br/>
    <input type="text" name="Comment"/>Comment<br/>
    <input type="submit" value="Save" class="button" id="btClick"/>
</form>

和脚本:

$(document).ready(function () {
    $("#temp1").validate({
        rules: {
            HospitalFinNumber: {
                required: true,
                minlength: 6
            },
            DateOfBirth: {
                required: true
            },
            AdmitDate: {
                required: true
            },
            Comment: {
                required: function (element) {
                    return $(element).val().length < 4000;
                },
                maxlength: 4000
            }
        },
        messages: {
            HospitalFinNumber: 'Please Enter a Hospital Fin number',
            DateOfBirth: 'Please enter a valid Date Of Birth',
            AdmitDate: 'Please select an Admit Date',
            Comment: 'Why dont you stop attempting to put in more than 4000 characters?  Thanks...'
        }
    });
});

jQuery.validate uses names for the rules, not ids. So replace the ids with names in your input fields.

Also your form should be <form id="temp1"> and not <form id="#temp">.

Here's a link to your fixed code:

<form id="temp1">
    <input type="text" name="HospitalFinNumber"/> Hospital Fin<br/>
    <input type="text" name="DateOfBirth"/>Date of Birth<br/>
    <input type="text" name="AdmitDate"/>Admit Date<br/>
    <input type="text" name="Comment"/>Comment<br/>
    <input type="submit" value="Save" class="button" id="btClick"/>
</form>

and the script:

$(document).ready(function () {
    $("#temp1").validate({
        rules: {
            HospitalFinNumber: {
                required: true,
                minlength: 6
            },
            DateOfBirth: {
                required: true
            },
            AdmitDate: {
                required: true
            },
            Comment: {
                required: function (element) {
                    return $(element).val().length < 4000;
                },
                maxlength: 4000
            }
        },
        messages: {
            HospitalFinNumber: 'Please Enter a Hospital Fin number',
            DateOfBirth: 'Please enter a valid Date Of Birth',
            AdmitDate: 'Please select an Admit Date',
            Comment: 'Why dont you stop attempting to put in more than 4000 characters?  Thanks...'
        }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文