带有多个具有相同名称的文本框和具有相同名称的对应复选框的jQuery验证 [英] Jquery Validation with multiple textboxes with same name and corresponding checkboxes with same name

查看:146
本文介绍了带有多个具有相同名称的文本框和具有相同名称的对应复选框的jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选中了具有相同名称的多个文本框时,如何为它们添加Jquery验证

How can I add Jquery Validation for multiple textboxes with same name, when their corresponding checkboxes are checked

    Jquery:
     $("input:[name='abc']").change(function() {
                var method = $(this).val();
                if ($(this).is(':checked')) {
                    $("#" + method).spinner({
                        'disabled': false
                    });
                    console.log(method);
                    $("#" + method).rules("add",{
                            range: [1, 99],
                            required : true,
                            messages: {
                                    range: "Please enter a value between 1 and 99",
                                    required: "Please give a value for " + method
                            }
                    });
                    $("#" + method).val(1);
                } else {
                    $("#" + method).spinner({
                        'disabled': true
                    });
                    console.log(method);
                    $("#" + method).rules('remove','range');
                    $("#" + method).rules('remove','required');
                    $("#" + method).val(0);
                }
            });

尝试根据复选框更改事件在文本框中添加和删除规则.

Trying to add and remove rules to textboxes depending upon the checkbox change event.

推荐答案

如何为多个同名文本框添加jQuery验证 ...

您可以为此使用 jQuery Validate .

此插件内部使用name属性来跟踪所有表单元素,因此,所有name属性值必须是唯一的.对此硬编码要求没有解决方法.

This plugin internally uses the name attribute to keep track of all form elements, therefore, all name attribute values must be unique. There is no workaround to this hard-coded requirement.

这篇关于带有多个具有相同名称的文本框和具有相同名称的对应复选框的jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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