2012-05-14 100 views
0

我正在为客户端的应用程序工作。将需要特殊的验证编码,所以我不能像在其他几个应用程序中那样使用插件。验证文本的下拉字段时,我有这个代码工作,但是当我添加代码来验证复选框时,它破裂了。我在这个jQuery代码中做错了什么? 我的jsfiddlehttp://jsfiddle.net/justmelat/7N7bw/jquery - 为什么没有:复选框:选中验证不工作

完整的jQuery代码:>>

$(document).ready(function(){ 
    $("#btnCatchReqFlds").on('click', function(){ 
     $("#holdErrMsg").empty(); 
     var reqButEmpty = $('fieldset:visible').find('input[type="text"][class*="-required"],textarea[class*="-required"],select[class*="-required"]').filter(function() 
      { 
        return $.trim($(this).val()) === ""; 
      }); 
     var chkbx_reqButEmpty = $('fieldset:visible').find('input[type="checkbox"][class*="-required"]').filter(function() 
      { 
        return !$(this).is(':checked') 
      }); 
        var holdAll = reqButEmpty + chkbx_reqButEmpty; 
        if(holdAll.length > 0) 
         { 
          holdAll.each(function() { 
           $('#holdErrMsg').append("Please fill in the " + this.name + "<br />"); 
          }); 
         } 
        return !holdAll.length; 
       }); 
     }); 

上面工作,直到我加入此复选框验证:>>

var chkbx_reqButEmpty = $('fieldset:visible').find('input[type="checkbox"][class*="-required"]').filter(function() 
      { 
        return !$(this).is(':checked') 
      }); 
        var holdAll = reqButEmpty + chkbx_reqButEmpty; 

这里是jQuery的新建议 - 但不起作用>>

$(document).ready(function(){ 
    $("#btnCatchReqFlds").on('click', function(){ 
     $("#holdErrMsg").empty(); 
     var reqButEmpty = $('fieldset:visible').find('input[type="text"][class*="-required"],textarea[class*="-required"],select[class*="-required"]').filter(function() 
      { 
        return $.trim($(this).val()) === ""; 
      }); 
     //var chkbx_reqButEmpty = $('fieldset:visible').find('input[type="checkbox"][class*="-required"]').filter(function() 
     var chkbx_reqButEmpty = $('fieldset:visible').find('input:checked[class*="-required"]').filter(function() 
      { 
        console.log(this); 
        //return !$(this).is(':checked') 
        //return !this.checked; 
        return $(this).is(!':checked'); 
      }); 
        //var holdAll = reqButEmpty + chkbx_reqButEmpty; 
        //var holdAll = reqButEmpty.concat(chkbx_reqButEmpty).length; 
        var holdAll = $.extend(reqButEmpty,chkbx_reqButEmpty); 
        if(holdAll.length > 0) 
         { 
          holdAll.each(function() { 
           $('#holdErrMsg').append("Please fill in the " + this.name + "<br />"); 
          }); 
         } 
        return !holdAll.length; 
       }); 
     }); 

回答

0

您是否使用(input:checked)在您的查找功能中尝试?

$('fieldset:visible').find('input:checked[class*="-required"]').filter(function(){}); 

编辑:

不应这一行:return !$(this).is(':checked')return $(this).is(':checked')

编辑:

if(!$(this)is(':checked') 
    return true/false; 
+0

现在我得到这个错误[遗漏的类型错误:对象[对象的对象] [对象对象]没有方法'each'],但我没有使用.each因为is()应该检查是否有任何框被选中。 – user1176783

+0

@ user1176783阅读我的答案以摆脱错误。 – VisioN

+0

我想赶上所需的领域,但没有填写,所以在这种情况下,它应该不是检查 – user1176783

0

尝试

return $(this).is(!':checked') 
0

有符合一个问题:

var holdAll = reqButEmpty + chkbx_reqButEmpty; 

通过连接两个JQuery的对象,你会得到类型[Object][Object]

使用此代码来代替:

var holdAll = $.extend(reqButEmpty,chkbx_reqButEmpty); 

DEMO:http://jsfiddle.net/7N7bw/2/

0

你可以试试:

var chkbx_reqButEmpty = $('fieldset:visible').find('input[type="checkbox"][class*="-required"]').filter(function() { 
     return !this.checked; 
}); 
var holdAll = reqButEmpty.concat(chkbx_reqButEmpty).length;