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

warning: 这篇文章距离上次修改已过356天,其中的内容可能已经有所变动。

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

记录实际业务遇到问题

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

//用于生成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验证

$(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
            })
        });
    });

    
版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/142.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

JS
最后修改于:2023年04月28日 13:34

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png