checking each required input for empty value with jQuery

后端 未结 5 1774
感情败类
感情败类 2020-12-19 11:36

I am checking for each required fields in a form. I tried this but it only works for the first input. How can I make it work for each input?

if($(\'[require         


        
5条回答
  •  执念已碎
    2020-12-19 12:13

    You need to grab the value of each element, and if ALL of them are not empty, do the first thing, and if ANY of them are empty, do the second thing, correct?

    [untested]

    // test if any of the values are not empty
    var is_empty = false;
    $('[required]').each( function(idx, elem) {
        is_empty = is_empty || ($(elem).val() == '');
    });
    
    // now do the thing, but only if ALL the values are not empty
    if ( ! is_empty) {
        $('.button1').fadeIn(0);
        $('.button2').fadeOut(0);
    }else{
        $('.button1').fadeOut(0);
        $('.button2').fadeIn(0);
    }
    

提交回复
热议问题