【发布时间】:2016-07-12 16:59:41
【问题描述】:
我有一个 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");
} 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">
<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">
<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">
<div class="error target_weight_error">This field is required!</div>
</div>
</div>
<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">
<div class="error size_error">This field is required!</div>
</div>
</div>
</div>
【问题讨论】:
标签: jquery forms validation input