使用数组验证为true

时间:2017-11-01 14:38:28

标签: javascript jquery arrays loops verify

我正在尝试创建一个循环来验证.offer_no span中找到的所有数字都不是= 0并且如果所有数字都是0则返回true。目前我已写过,但我不知道如何创建验证循环。

$(".offers_container").find(".offer_no span").text()

Console Screenshot

1 个答案:

答案 0 :(得分:0)

像这样:

//all zeros exaple:

   function is_all_zeros(){                                       //default function return
     var out=true;
     if($(".offers_container").find(".offer_no span").length<1){  //if not found elements return false
     	 out=false;
     }
     $(".offers_container").find(".offer_no span").each(function(){
     	var this_text_int=parseInt($(this).text(), 10);           //integer value of found spin
        if(this_text_int!=0){                                     //found value not 0
        	 out=false;
        }
     });
     return out;
   }
   
   
   
   console.log(is_all_zeros());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class="offers_container">
   <div class="offer_no">
       <span>0</span>
   </div>
</div>

<div class="offers_container">
   <div class="offer_no">
       <span>0</span>
   </div>
</div>

//not all zerros example:

   function is_all_zeros(){                                       //default function return
     var out=true;
     if($(".offers_container").find(".offer_no span").length<1){  //if not found elements return false
     	 out=false;
     }
     $(".offers_container").find(".offer_no span").each(function(){
     	var this_text_int=parseInt($(this).text(), 10);           //integer value of found spin
        if(this_text_int!=0){                                     //found value not 0
        	 out=false;
        }
     });
     return out;
   }


   console.log(is_all_zeros());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class="offers_container">
   <div class="offer_no">
       <span>0</span>
   </div>
</div>

<div class="offers_container">
   <div class="offer_no">
       <span>4</span>
   </div>
</div>