I want check if there is class .required_selectbox in html code run function required_selectbox() and if there is class .required in html code run function required_valid(), after submit, how is it?
function required_valid() {
$('.required').each(function () {
if (!$(this).val()) {
//var cssObj=;
$(this).css("background", "#ffc4c4");
result = false;
} else {
$(this).css("background", "#FFFFEC");
result = true;
}
$(this).keyup(function () {
$(this).css("background", "#FFFFEC");
})
});
return result;
}
function required_selectbox() {
$('.required_selectbox').each(function () {
if (!$(this).val()) {
//var cssObj=;
$(this).css("background", "#ffc4c4");
result = false;
} else {
result = true;
}
});
return result;
}
$('.submit').submit(function () {
//alert('hello');
var passed = true;
passed = required_selectbox() && passed;
passed = required_valid() && passed;
if (!passed) {
return false;
}
});
A simple approach would be to run those functions even if the elements aren’t there. Because no elements would be found, the calls to
each()would do nothing, so all that’s needed is to have theresultvariable set totrueby default:Also, it looks like you probably don’t want the
elsestatement in there so you might want to remove it.