jQuery-将验证规则动态添加到多个文本框 [英] jQuery - Dynamically adding validation rule to multiple textboxes

查看:67
本文介绍了jQuery-将验证规则动态添加到多个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将验证规则动态添加到多个文本框.这是js:

I'm trying to add a validation rule to multiple textboxes dynamically. Here's the js:

            //validate form.
            $("#SubmitForm").validate();
            $("input[id*=Hours]").rules("add", {
                number: true,
                messages: {
                    number: "Please enter a valid Hours"
                }
            });

这会将规则应用于ID中带有小时"的页面上的第一个文本框,但随后不会将其应用于其他任何文本框.

This applies the rule to the very first textbox on the page with "Hours" in the id but then it doesn't apply it to any of the other ones.

有人知道这是怎么回事吗?

Anyone know what's wrong here?

谢谢, 贾斯汀

推荐答案

您可以使用

You can add the rule to each item matched by the wildcard selector with .each(), like this:

$("#SubmitForm").validate();
$("input[id*=Hours]").each(function() {
    $(this).rules("add", {
        number: true,
        messages: {
            number: "Please enter a valid Hours"
        }
    });
});

希望这会有所帮助!

这篇关于jQuery-将验证规则动态添加到多个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆