How do I use jQuery to disable a form's submit button until every required field has been filled?

前端 未结 6 1645
故里飘歌
故里飘歌 2021-01-01 08:21

I have a form with multiple inputs, select boxes, and a textarea. I would like to have the submit button be disabled until all of the fields that I designate as required are

6条回答
  •  粉色の甜心
    2021-01-01 08:44

    Here's something comprehensive, just because:

    $(document).ready(function() {
        $form = $('#formid'); // cache
        $form.find(':input[type="submit"]').prop('disabled', true); // disable submit btn
        $form.find(':input').change(function() { // monitor all inputs for changes
            var disable = false;
            $form.find(':input').not('[type="submit"]').each(function(i, el) { // test all inputs for values
                if ($.trim(el.value) === '') {
                    disable = true; // disable submit if any of them are still blank
                }
            });
            $form.find(':input[type="submit"]').prop('disabled', disable);
        });
    });
    

    http://jsfiddle.net/mblase75/xtPhk/1/

提交回复
热议问题