Я пытаюсь проверить форму с помощью jQuery. Я выбираю все поля формы, которые имеют class="required"
, и используя функцию .each()
, чтобы проверить, пустое поле. Если он пуст, я показываю скрытый div, расположенный относительно поля.Показать скрытый div с jquery .each() function
Что-то явно не так, как следующий код не удается:
$('#application').submit(function(){
var requiredFields = $('.required').val();
requiredFields.each(function(){
if (requiredFields == ''){
position = requiredFields.position();
width = requiredFields.outerWidth();
$("#error-wrapper-mail").css({
visibility: "visible",
top: (pos.top - 8) + "px",
left: (pos.left + width + 25) + "px"
}).hide().fadeIn("slow").delay(2700).fadeOut("slow");
requiredFields.addCLass("not-valid").focus().delay(3500).queue(function(){
$(this).removeClass("not-valid");
});
return false;
}
}); });
Если я выбираю поле ввода с помощью ID, остальная часть кода работает отлично.
Разметка:
<form role="form" id="application" action="add-customer.php" method="post" enctype="multipart/form-data">
<label for="Input-firstname">First name<span class="redstar"> * </span></label>
<input type="text" class="form-control required" id="Input-firstname" name="firstname" placeholder="Your first name">
<br />
<label for="Input-lastname">Last name<span class="redstar"> * </span></label>
<input type="text" class="form-control required" id="Input-lastname" name="lastname" placeholder="Your last name">
<br />
<label for="InputPass">Password<span class="redstar"> * </span></label>
<input type="password" class="form-control required" id="InputPass" name="pass" placeholder="Choose your password">
<span class="help-block">Please create a password more than 6 symbols long, including uppercase letters, lowercase letters and digits.</span>
...
</form>
Я новичок в JQuery, и я буду благодарностью, если кто может мне точку в правильном направлении
EDIT: Вот скрипка - http://jsfiddle.net/7yLmq/
Только примечание стороны, его 'addClass' вместо' addCLass' – Satpal