Я хочу добавить/удалить класс css для некоторых входов из формы
<form id="FormId" method="post" enctype="multipart/form-data">
<input type="text" class="input-bg">
<input type="text" class="input-bg">
<input type="text" class="input-bg">
<input type="text" class="validate input-bg">
<input type="text" class="validate input-bg">
<input type="text" class="input-bg">
<input type="text" class="bg-datepicker">
</form>
$("#FormId input[type=text]").each(function ()
{
<!--if input have class validate I want to verify if text !=''-->
input.addClass('error');
<!--else-->
input.removeClass('error');
})
как проверить здесь значение?
Попробуйте это, используйте .filter()
$("#FormId input[type=text].validate").filter(function(){
this.value ==''?$(this).addClass('error'): $(this).removeClass('error')
});
Просто зациклируйте входы с классом validate
. Вы можете использовать toggleClass
чтобы добавить или удалить класс error
, передав логическое значение в качестве второго аргумента:
$('#FormId input[type="text"].validate').each(function() {
$(this).toggleClass('error', !$.trim(this.value).length);
});
$("#FormId input[type=text]").each(function () {
if ($(this).hasClass('validate')) {
if (this.value == '') {
$(this).addClass('error');
} else if ($(this).hasClass('error')) {
$(this).removeClass('error');
}
}
});
или
$("#FormId input[type=text].validate").each(function () {
if (this.value == '') {
$(this).addClass('error');
} else if ($(this).hasClass('error')) {
$(this).removeClass('error');
}
});
Справка