简体   繁体   中英

Having trouble with jquery each function with if statement

I'm having trouble with one jquery each function combined with else if statement.

I'm having several inputs with my worktime page. I'd like to display alert everytime if one of the inputs have 00:00 value.

Here's some HTML

<div class="fpp-plandnia">
            <div class="fpp-plandnia-item">
                <div class="fpp-godziny-container">
                    <div class="_grid100">
                        <div class="_grid50">
                                <input id="godzina_noiphone_od" type="text" value="07:00">
                        </div>
                        <div class="_grid50 r">
                                <input id="godzina_noiphone_do" type="text" value="15:00">          
                        </div>
                    </div>
                </div>

         <div class="fpp-plandnia-item">
                <div class="fpp-godziny-container clearfix">
                    <div class="_grid100">
                        <div class="_grid50">
                                <input id="godzina_noiphone_od" type="text" value="07:00">
                        </div>
                        <div class="_grid50 r">
                                <input id="godzina_noiphone_do" type="text" value="15:00">
                        </div>
                    </div>

                </div>

and my script

$('.fpp-plandnia-item div.fpp-godziny-container').each(function(){

   var a = $(this).find('div._grid50 input#godzina_noiphone_od').val()
   var b = $(this).find('div._grid50.r input#godzina_noiphone_do').val()

   if ((a === '00:00') || (b === '00:00')) {      
    alert('Hour times can not be 00:00');        
   } else {        
     Save();            
   }     
});

No it displays alert but does not prevent Save() function.

Many thanks for your help.

Best

Maciej

I figured it out.

Now it works like a charm

var time_test = false;


$('.fpp-plandnia-item div.fpp-godziny-container').each(function(){

     var a = $(this).find('div._grid50 input#godzina_noiphone_od').val()
   var b = $(this).find('div._grid50.r input#godzina_noiphone_do').val()


    if ((a === '00:00') || (b === '00:00')) {
        time_test = true;
    } else {
        //
    }

});


if (time_test) {

    alert('Hour times can not be 00:00');

} else {

    Save();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM