Я немного запутался с возвращаемыми значениями функции. У меня есть форма, и в ней есть текстовые поля и переключатели. Я проверяю все текстовые поля вместе и переключатели вместе. Таким образом, у меня теперь есть 2 значения возврата из этих 2 функций. Могу ли я использовать одну и ту же возвращаемую переменную для этих двух функций. Это мой код
$(function() {
$('#submitBtn').click(function(){
var returnValue = true;
//Validating radio buttons required
//Code of checking radio buttons
if( !unchecked.is(':checked') ) {
alert("Required field");
returnValue = false;
}
});
//Other Required fields
$('.required_text').filter(':visible').each(function () {
var input = $(this);
if (!$(this).val()) {
alert("Required field");
returnValue = false;
}
});
alert(returnValue);
return returnValue;
});
}
});
Если я использую разные возвращаемые переменные для каждой из этих функций, таких как radioReturn и textReturn, а в последнем случае, если я использую
if(radioReturn && textReturn){
returnValue = true;
}
else{
returnValue = false;
}
Но я не хочу использовать слишком много переменных. Так что я могу использовать только одну возвращаемую переменную и обрабатывать форму submit.
благодаря
Да, ты можешь
Попробуйте это
$(function() {
// all validation inside the click event
$('#submitBtn').click(function(){
// initiate the value as true
var returnValue = true;
//Validating radio buttons required
//Code of checking radio buttons
if( !unchecked.is(':checked') ) {
alert("Required field");
// add css to highlight error
returnValue = false;
}
// check the input fields only if returnValue is true
// if u want to highlight all the errors then this check no need
if(returnValue)
{
$('.required_text').filter(':visible').each(function () {
var input = $(this);
if (!$(this).val()) {
// add css to highlight error
alert("Required field");
returnValue = false;
}
});
}
//Other Required fields
alert(returnValue);
return returnValue;
});
});
ОБНОВЛЕНИЕ 2:
$('#submitBtn').click(function(){
// initiate the value as true
var returnValue = true;
//Validating radio buttons required
//Code of checking radio buttons
if( !unchecked.is(':checked') ) {
// add css to highlight error or
// add label nearby it to said Required
returnValue = false;
}
// check the input fields only if returnValue is true
// if u want to highlight all the errors then this check no need
$('.required_text').filter(':visible').each(function () {
var input = $(this);
if (!$(this).val()) {
// add css to highlight error or
// add label nearby it to said Required
returnValue = false;
}
});
//Other Required fields
// here if u have any errors then the return value always false
alert(returnValue);
return returnValue;
});
alert("required field")
ничего не говорит пользователю о том, что это за поле!