How to loop through elements of forms with JavaScript?

后端 未结 7 1300
無奈伤痛
無奈伤痛 2020-12-05 01:53

I have a form:

相关标签:
7条回答
  • 2020-12-05 02:34
    $(function() {
        $('form button').click(function() {
            var allowSubmit = true;
            $.each($('form input:text'), function(index, formField) {
                if($(formField).val().trim().length == 0) {
                    alert('field is empty!');
                    allowSubmit = false;
                }
            });
            return allowSubmit;
        });
    });
    

    DEMO

    0 讨论(0)
提交回复
热议问题