1

我有一个下拉菜单,其中包含必需、选项等条件,并且隐藏了大约 8 个文本字段。即使字段被隐藏,也很少有字段得到验证并抛出错误消息。如何避免这种情况?请帮助解决这个问题。

我的代码是:

            function responseFunction(res) {
    //1:mandatory
    //2:optional
    //3:hidden
    var str = res.split("~");
   for (i in str)
    {
     var field= str[i].split(':');
            switch (field[0]) {
            case "BillingAddressLine1": 
                 switch (field[1]) {                    
                  case "1":$('#ContentSection_lblBillingInfoAddress').prepend('<span>*</span>');
                  $("#<%= txtBillingInfoAddress.ClientID %>").rules("add", {required  : true, messages : {
                  required    : 'Please Enter Address'   }});
                  break;
                  case "3":$('#ContentSection_lblBillingInfoAddress, #ContentSection_txtBillingInfoAddress').hide().parent('p').css("paddingTop", "0px");
                  break;
                 }
            break;
            case "BillingFullName": 
                 switch (field[1]) {
                  case "1":$('#ContentSection_lblBillingInfoAccountHolderName').prepend('<span>*</span>');
                   $("#<%= txtBillingInfoAccountHolderName.ClientID %>").rules("add", {required  : true, messages : {
                  required    : 'Please Enter Account Holder Name'   }});
                  break;
                  case "3":$('#ContentSection_lblBillingInfoAccountHolderName, #ContentSection_txtBillingInfoAccountHolderName').hide().parent('p').css("paddingTop", "0px");
                  break;
                 }
            break;
    }
    }

我的截图是: https ://www.dropbox.com/sh/asmpnuiqqlo40us/S9yHCuNSyl?m#f:errorThrowing.jpg

4

4 回答 4

2

为所有隐藏元素添加一个类属性并使用该ignore属性忽略所有这些元素:

<input type='hidden' class='hiddenClass' />

$('form').validate({
 ignore: '.hiddenClass'
});
于 2012-08-17T07:30:28.403 回答
1

隐藏时删除字段验证

$('#targetId').rules('remove');

可见时重新添加验证

$('#targetId').rules('add', {
    required: true
});
于 2012-08-17T07:31:48.310 回答
1

只需检查下面的案例陈述

var control = $("#<%= txtBillingInfoAddress.ClientID %>");
if($(control).is(':hidden')){
  // remove validation
}
于 2012-08-17T07:45:28.827 回答
1

从 javascript 方法中隐藏验证,例如:

function lalala()
{
  var validator1 = document.getElementById('Validator1ClientID');
  ValidatorEnable(validator1, false); 
}
于 2013-02-06T14:29:53.073 回答