前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >解决validate无法验证多个相同name的input

解决validate无法验证多个相同name的input

作者头像
用户9131103
发布2023-07-17 19:29:14
2550
发布2023-07-17 19:29:14
举报
文章被收录于专栏:工作经验工作经验

解决validate无法验证多个相同name的input (动态生成的表格)

记录实际业务遇到问题

首先给input分别设置不同的id

代码语言:javascript
复制
//用于生成uuid
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    function guid() {
        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
    }
//为动态添加的元素绑定事件
    $('#contentTable').on('click', '.addTable', function () {
        // var uuid = "qty" + guid();
        //步骤预设
        var str1 = ' <tr>\n' +
            '                                <td>\n' +
            '                                    <input id="qty' + guid() + '" type="text" style="width: 126px;height: 32px" class="input-xlarge required"\n'
            +
            '                                           name="gspStepTaskPresetChildtable.stepName"></input>\n' +
            '                                </td>\n' +
            '                                <td>\n' +
            '                                    <input id="qty' + guid() + '" type="text" style="width: 126px;height: 32px" class="input-xlarge required"\n'
            +
            '                                           name="gspStepTaskPresetChildtable.workContent"></input>\n' +
            '                                </td>\n' +
            '                                <td>\n' +
            '                                    <input id="qty' + guid() + '"  type="text" style="width: 126px;height: 32px" class="input-xlarge required"\n'
            +
            '                                       onkeyup="value=value.replace(/[^\\d\\.]/g,\'\')"     name="gspStepTaskPresetChildtable.completeDays"></input>\n' +
            '                                </td>\n' +
            '                                <td>\n' +
            '                                    <input  id="qty' + guid() + '" type="text" style="width: 126px;height: 32px" class="input-xlarge required"\n'
            +
            '                                           name="gspStepTaskPresetChildtable.milestone"></input>\n' +
            '                                </td>\n' +
            '                                <td>\n' +
            '                                    <input id="qty' + guid() + '" type="text" style="width: 126px;height: 32px" class="input-xlarge required"\n'
            +
            '                                           name="gspStepTaskPresetChildtable.precautions"></input>\n' +
            '                                </td>\n' +
            '                                <td>\n' +
            '                                    <input id="qty' + guid() + '" type="text" style="width: 126px;height: 32px" class="input-xlarge required"\n'
            +
            '                                      onkeyup="value=value.replace(/[^\\d\\.]/g,\'\')"      name="gspStepTaskPresetChildtable.standardWorkingHours"></input>\n' +
            '                                </td>\n' +
            '                                <td>\n' +
            '                                   <p class="addTable">+</p>\n' +
            '                                   <p class="delTable" onclick="delTable(this);">-</p>\n' +
            '                                </td>\n' +
            '                            </tr>';

        $('#contentTable > tbody:nth-child(2)').append(str1);

    });

设置validate的多个input验证方法为根据id验证

代码语言:javascript
复制
$(function () {
        if ($.validator) {
            $.validator.prototype.elements = function () {
                var validator = this,
                    rulesCache = {};
                return $([]).add(this.currentForm.elements)
                    .filter(":input")
                    .not(":submit, :reset, :image, [disabled]")
                    .not(this.settings.ignore)
                    .filter(function () {
                        var elementIdentification = this.id || this.name;
                        !elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor name assigned", this);
                        if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
                            return false;
                        rulesCache[elementIdentification] = true;
                        return true;
                    });
            };

        }
        # [id^=qty] 根据实际业务需求修改 id以qty开头的
        $('[id^=qty]').each(function (e) {
            $(this).rules('add', {
                minlength: 2,
                required: true
            })
        });
    });
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年01月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 首先给input分别设置不同的id
  • 设置validate的多个input验证方法为根据id验证
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档