How can I verify that all input of the same class have a value of 0

0

I need to evaluate if all the input with the class "hour" have the value of 0, I thought about doing it with each but it does not work for me.

if ($('.productTextInput').filter(function() {
}).length === 0) {
    console.log("Todos en 0");
}
else {
    console.log("algunos diferentes de 0")
}
    
asked by Fernando Garcia 22.10.2018 в 19:39
source

3 answers

2

With each you can do it this way:

var bandera = false;
$(".hour").each(function(){
    if ( $(this).val() !== '0' )
     bandera = true;
});
if (bandera)
   alert("Hay datos distintos");
else
   alert("Son ceros");

The only thing that you must define correctly is your selector so that the event makes the correct match of the elements you want.

$(".send").click(function(){
    var bandera = false;
    $(".hour").each(function(){
        if ( $(this).val() !== '0' )
         bandera = true;
    });
    if (bandera)
       alert("Hay datos distintos");
    else
       alert("Son ceros");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="0" class="hour">
<input type="text" value="0" class="hour">
<input type="text" value="0" class="hour">
<input type="text" value="0" class="hour">
<input type="text" value="0" class="hour">
<input type="button" value="ver" class="send">

Note : Change the values of the inputs to see the results

    
answered by 22.10.2018 в 19:46
0

You can also check with this example, using a filter on your array of input elements you can check when one of them has a value different from 0.

$('#check').on('click', function(){
	//Obtienes todos los campos cuyo valor sea diferente a 0
	var elements = $('.hour').filter(function(index,item){
			return parseInt(item.value) !== 0;
	});
        //Puede ser de esta manera tambien un poco mas simplificada
        //var elements = $('.hour').filter((index,item) => parseInt(item.value) !== 0);

	//Si hay elementos que tienen valores diferentes a 0...
	if(elements.length > 0){
		alert('Hay elementos diferentes de 0');
	} else {
		alert('No Hay elementos diferentes de 0');
	}
});	
<input type="text" class="hour" value="1"/>
<input type="text" class="hour" value="0"/>
<input type="text" class="hour" value="0"/>
<input type="text" class="hour" value="0"/>
<input type="text" class="hour" value="0"/>
<input type="text" class="hour" value="0"/>
<input type="button" value="Comprobar" id="check" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
    
answered by 22.10.2018 в 20:28
0

You would be missing the condition of the filter, for each element the element is returned if and only if the value is not 0 and so the final set will contain those that are not 0. something like this:

$("#dale").click(function() {
  if (
    $('.productTextInput').filter(function(idx, el) {
      return 0 !== parseInt($(el).val())
    }).length === 0
  ) {
    console.log("Todos en 0");
  } else {
    console.log("algunos diferentes de 0")
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="0" class="productTextInput">
<input type="text" value="0" class="productTextInput">
<input type="text" value="0" class="productTextInput">
<input type="text" value="0" class="productTextInput">
<input type="text" value="0" class="productTextInput">
<button id="dale">verificar</button>
    
answered by 22.10.2018 в 20:30