【问题标题】:jquery validate interdependent PAIR of FIELDSjquery验证FIELDS的相互依赖的PAIR
【发布时间】:2011-11-18 18:00:20
【问题描述】:

我需要验证表单中的一组姓名、手机号码字段。为了方便服务器端脚本,我用数组表示法(即,用方括号)命名了这些字段。 此外,下面的相同 html 会根据需要多少条记录重复多次。

<table width="100%" border="0">
 <tr><td class="b_name"><label>Name <sup>*</sup></label></td><td class="b_mobile"><label>Mobile <sup>*</sup></label></td><td class="removetext"><a href="javascript:void(0);" onclick="removebroadcast(this);" id="remove_<?php echo $cnt;?>">Remove</a></td></tr>
 <tr><td class="b_name" id="name_<?php echo $cnt;?>"><input type="text" value="" name="broadcast_name[]" class="broadcast_name" /></td><td class="b_mobile" id="mobile_<?php echo $cnt;?>"><input type="text" value="" name="broadcast_mobile[]" class="broadcast_num" /></td><td id="message_<?php echo $cnt;?>">&nbsp;</td></tr>
</table>

到目前为止,关于规则/消息部分,我想出了:

'broadcast_name[]':{required:function(element){
                    return $(element).parent().next().children('input').val()!='';
                }
            },
'broadcast_mobile[]':{required:function(element){
                    return $(element).parent().prev().children('input').val()!='';
                }
            },

但是,这仅适用于第一组名称/移动字段。我也尝试了一些其他技巧(例如,使用 $.each 类型验证,使用自定义规则函数等),但它们似乎都不起作用。我在这里做错了什么吗?请帮忙! :(

【问题讨论】:

    标签: jquery validation


    【解决方案1】:

    您可能想查看电话字段:http://digitalbush.com/projects/masked-input-plugin/

    你不能做类似的事情

    $('form').submit(function(e){
        var errors = false;
        $(this).find('input').each(function(){
            if($(this).val() === ''){
                errors = true;
            }
        });
        if(errors){
            e.preventDefault();
            alert('errors exist');
        }
    });
    

    【讨论】:

    • 感谢面具插件,但我实际上一直在电话领域使用它,只是认为没有必要提供所有细节。关于提供的解决方案 - 我需要具体使用 jquery 验证插件,如果您之前不清楚,请道歉。
    • 不客气,我想知道为什么我的回答对你不起作用
    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 2011-10-26
    • 2020-02-13
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多