2016-07-12 71 views
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>

回答

1

有在你的代码的几个错误,但主要问题是在这里:

if ($(this).val().trim().length == '') 

Length返回一个数字,你试图把它比作一个串。 我怀疑你的意思是:

if ($(this).val().trim() === '') 

或者

if ($(this).val().trim().length === 0) 

我删除从您的代码无关的元素,以获得在MVCE

这是你MVCE的工作演示:JS Fiddle Demo

JQuery的

$('#next').click(function() { //My next button 
    var isValid = true; 
    $('.personal-informations').each(function(i, obj) { //All field from my form 
    if ($(this).val().trim() === '') { //If this field is empty -> do... 
     /*Here I want to show the error field on every empty element*/ 
     $(this).css("display", "block"); 
     $(this).css('border-color', "#ff0000"); 
     isValid = false; 
     console.log(isValid); 
    } 
    }); 
    if (isValid) { 
    alert("All fields are filled!"); 
    } 
}); 

HTML

<div class="field personal-informations-icon"> 
    <div class="questions-fcm-3 styled-radio"> 
    <span class="title">age</span> 
    <div class="questions"> 
     <input id="age" class="personal-informations" pattern="[0-9]" name="age" placeholder="in Jahren" value="" min="1" aria-required="true" type="number"> 
     <div 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 id="weight" class="personal-informations" pattern="[0-9]" name="weight" placeholder="in KG" value="" min="1" aria-required="true" type="number"> 
     <div 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> 

<input type="button" id="next" value="Next"> 

CSS

.error { 
    display: none; 
} 

你的代码的其他意见:

  • 你有一个班级分配给你的输入。您应该在您的JQuery中使用该类 来绑定您的事件,而不是每个输入的id。
  • 你似乎没有意识到,每一个循环结束了 输入的迭代。在每个括号内,您只检查一个 特定输入。最简单的方法是将该输入作为 $(this)。另外,您需要跟踪您是否有空的 字段。我已经使用名为isValid的布尔值来完成此操作。这 布尔设置为false遇到空场时,然后 可以显示警报之前检查布尔值。
  • 如果你只需要一个字段的数字,你应该让用户知道。 要么把一个标签到一边,更改无效的消息状态 只允许数字,更改输入,使任何东西,但 一些,当我键入它会立即被删除,或者......什么的。 用户会想知道为什么要删除“11磅”条目并指出需要输入。
  • console.log()是你的朋友。用它来调试你自己的代码。
+0

OMG谢谢soooo多!!!你救了我的一天!现在我明白了情况:-) – Johnny97