我基本上有一个循环,循环通过一堆类确实有效,一个用于复选框,一个用于收音机盒。选择或检查字段时需要标记为必填字段(工作正常),我只需要为每种类型显示自定义错误消息:复选框和单选框。
我已经尝试在循环/方法中执行.validate(规则{必需:'请选择一个选项'})没有运气。我怎么能做到这一点?
代码:
// When the DOM is ready
$(document).ready(function() {
/* To add more boxes to hide/show, add the name of the field to the array,
set the box class to that name + Hide, set the class of the field to that name
then if radio box set value="Yes" Yes must have first letter capitlized */
// storing the class names of the HTML elements we want to mess with
var hiddenClassArray = [
"appliedWorkedYes",
"workStudyYes",
"workHistoryYes",
"workWeekEndsYes",
"cprYes",
"aedYes",
"aidYes",
"lifegaurd",
"wsiYes",
"gaurdYes",
"lifegaurdChk",
"fitnessChk",
"fitPTCYes",
"fitGrpYes",
"outdoorAdvChk",
"challengeChk",
"injuryCareChk",
"athTrainYes",
"serviceCenter",
"itDepartmentChk",
"marketingChk"
];
// looping over each array element, hiding them using jQuery
for(var i = 0; i < hiddenClassArray.length; i++){
// jQuery to append a display none.
$("."+hiddenClassArray[i]+"Hide").css("display","none");
}
// ************ RADIO & CHECK BOXES ************
// jQuery's Equlivant of a for each loop, a little fancier then that
$.each(hiddenClassArray, function(index, radio) {
// first is it a Check box?
if($("."+radio).is(':checkbox')) {
// when we click
$("." + radio).click(function() {
// if it's checked show
if($("."+ radio).attr('checked')){
// show
$("." + radio + "Hide").show('fast');
// make one of the group required
$("."+radio + "requried").addClass("required");
}
// default hide
else{
// hide
$("."+radio + "Hide").hide("fast");
// remove the required class attribute
$("."+radio + "requried").removeClass("required");
}
}); // ends .click
} // ends if
// if it's a radio box
else if ($("."+radio).is(':radio')) {
// On click
$("."+radio).click(function(){
// show
if($(this).val()==="Yes"){
// show
$("."+radio + "Hide").show("fast");
// make one of the group required
$("."+radio + "requried").addClass("required");
}
// default, hide
else{
// hide
$("."+radio + "Hide").hide("fast");
// remove the required class attribute
$("."+radio + "requried").removeClass("required");
}
}); // emds .click
}// ends else
}); // end .each
// Validating data
$(".empForm").validate();
}); // Ending the $(Doc) Ready