我正在为客户端的应用程序工作。将需要特殊的验证编码,所以我不能像在其他几个应用程序中那样使用插件。验证文本的下拉字段时,我有这个代码工作,但是当我添加代码来验证复选框时,它破裂了。我在这个jQuery代码中做错了什么? 我的jsfiddle:http://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;
});
});
现在我得到这个错误[遗漏的类型错误:对象[对象的对象] [对象对象]没有方法'each'],但我没有使用.each因为is()应该检查是否有任何框被选中。 – user1176783
@ user1176783阅读我的答案以摆脱错误。 – VisioN
我想赶上所需的领域,但没有填写,所以在这种情况下,它应该不是检查 – user1176783