checking each required input for empty value with jQuery

后端 未结 5 1783
感情败类
感情败类 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 12:18

    This?

    $( ':input[required]', yourForm ).each( function () {
        if ( this.value.trim() !== '' ) {
            // ...
        }
    });
    

    where yourForm is a reference to the FORM element (you can also use a selector here). This is the context - you only want to search for fields inside the form.

提交回复
热议问题