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

前端 未结 6 1627
故里飘歌
故里飘歌 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:34

    Start with the button disabled (obviously). Bind an onkeyup event to each required text input, and an onchange or onclick to the select boxes (and any radio buttons/checkboxes), and when it fires, check whether all required inputs are filled. If so, enable the button. If not, disable it.

    There is one loophole here, though. Users can delete the value of a text field without triggering the onkeyup event by using the mouse to "cut" the text out, or by holding down the delete/backspace key once they have deleted it all, and clicking the button before deleting it.

    You can get around the second by either

    • disabling the button with onkeydown and checking if it is ok on onkeyup
    • checking for validity when the button is clicked
    0 讨论(0)
  • 2021-01-01 08:35

    Set the disabled attribute on the submit button. Like:

    $('input:submit').attr('disabled', 'disabled');
    

    And use the .change() event on your form fields.

    0 讨论(0)
  • 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/

    0 讨论(0)
  • 2021-01-01 08:46

    Guess my first instinct would be to run a function whenever the user starts modifying any of the inputs. Something like this:

    $('#submitBtn').prop('disabled', true);
    $('.requiredInput').change(function() {
       inspectAllInputFields();
    });
    

    We then would have a function that checks every input and if they're validated then enable the submit button...

    function inspectAllInputFields(){
         var count = 0;
         $('.requiredInput').each(function(i){
           if( $(this).val() === '') {
               //show a warning?
               count++;
            }
            if(count == 0){
              $('#submitBtn').prop('disabled', false);
            }else {
              $('#submitBtn').prop('disabled', true);
            }
    
        });
    }
    

    You may also want to add a call to the inspect function on page-load that way if the input values are stored or your other code is populating the data it will still work correctly.

     inspectAllInputFields();
    

    Hope this helps, ~Matt

    0 讨论(0)
  • 2021-01-01 08:49

    An idea from me:

    • Define a variable -with global scope- and add the value true- Write a submit function within your check the value above varibale. Evalue the the submit event only, if the value is true.
    • Write a function which ckecks all value from input fields and select fields. Checking the length of value to zero. if the value length of one field zero then change the value of the global variable to false.
    • After that, add to all input fields the event 'onKeydown' or 'onKeyUp' and to all select boxes the event 'onChange'.
    0 讨论(0)
  • 2021-01-01 08:52

    I recommend taking a slightly different approach and using jquery's validation http://docs.jquery.com/Plugins/validation. The tactic you are suggesting is prone to security holes. The user could easily using firebug enable that button and then submit the form.

    Using jquery validation is clean and it allows you to show error messages under the required fields if so desired on submit.

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