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

2023-07-17 19:29:14 浏览数 (1)

解决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
            })
        });
    });

0 人点赞