1
我有一个jQuery代码看起来应该在我的形式,如果有任何空的输入。如果所有的输入都是空的,我的代码就可以工作,但是当我在一个输入中输入一个值时,它就不再工作了。请问你能帮帮我吗?谢谢!检查是否一个输入是空的(验证表单)
jQuery(document).ready(function($) {
$('#next').click(function() {//My next button
$('#age, #weight, #target_weight, #size').each(function() {//All field from my form
if ($(this).val().trim().length == '') {//If all fields are empty -> do...
$("html, body").animate({scrollTop: $('.progressbar-header').offset().top-100}, 250);
/*Here I want to show the error field on every empty element*/
$(this).closest('.questions').find('.error').css("visibility","visible");
$(this).css('border-color', "#ff0000");
\t \t \t \t
\t } else {
alert("All fields are filled!");
//This will show the next hidden div and hide the last one
$('.active').removeClass('active').fadeIn().hide()
.next().show().addClass('active');
return false;
}
});
});
});
<div class="field personal-informations-icon">
<div class="questions-fcm-3 styled-radio">
<span class="title">age</span>
<div class="questions">
<input style="border-color: rgb(255, 0, 0);" id="age" class="personal-informations" pattern="[0-9]" name="age" placeholder="in Jahren" value="" min="1" aria-required="true" type="number">
\t <div style="visibility: visible;" class="error age_error">This field is required!</div>
</div>
</div>
<div class="questions-fcm-3 styled-radio">
<span class="title">weight</span>
<div class="questions">
<input style="border-color: rgb(255, 0, 0);" id="weight" class="personal-informations" pattern="[0-9]" name="weight" placeholder="in KG" value="" min="1" aria-required="true" type="number">
\t <div style="visibility: visible;" class="error weight_error">This field is required!</div>
</div>
</div>
<div class="questions-fcm-3 styled-radio">
<span class="title">target weight</span>
<div class="questions">
<input id="target_weight" class="personal-informations" pattern="[0-9]*" name="aim-weight" placeholder="in KG" value="" min="1" aria-required="true" type="number">
\t <div class="error target_weight_error">This field is required!</div>
</div>
</div>
\t \t \t \t \t \t \t \t \t \t \t \t <div class="questions-fcm-3 styled-radio">
<span class="title">Body Size</span>
<div class="questions">
<input id="size" class="personal-informations" pattern="[0-9]*" name="size" placeholder="in cm" value="" min="1" aria-required="true" type="number">
\t <div class="error size_error">This field is required!</div>
</div>
</div>
</div>
OMG谢谢soooo多!!!你救了我的一天!现在我明白了情况:-) – Johnny97