Enable submit button only when all fields are filled

前端 未结 5 1115
-上瘾入骨i
-上瘾入骨i 2020-12-16 05:06

In my form, I want to enable form only when all fields (and radio button list) has been selected.

So far, I have successfully made the submit button enable

5条回答
  •  北海茫月
    2020-12-16 05:23

    yet another solution

    required = function(fields) {
      var valid = true;
      fields.each(function () { // iterate all
        var $this = $(this);
        if (($this.is(':checkbox') && !$this.is(":checked")) || // checkbox
           (($this.is(':text') || $this.is('textarea')) && !$this.val()) || // text 
           ($this.is(':radio') && !$('input[name='+ $this.attr("name") +']:checked').length)) {
                valid = false;
            }
        });
        return valid;
    }
    
    validateRealTime = function () {
        var fields = $("form :input:not(:hidden)"); // select required
        fields.on('keyup change keypress blur', function () {
            if (required(fields)) {
                {subnewtopic.disabled = false} // action if all valid
            } else {
                {subnewtopic.disabled = true} // action if not valid
            }
        });
    }
    

    http://jsfiddle.net/jsq7m8hL/2/

提交回复
热议问题