Force user to fill all fields before enabling form submit

前端 未结 12 2028
忘掉有多难
忘掉有多难 2021-02-07 00:49

I have a form containing various fields.

See jsFiddle demo.

My aim is to enable the submit button only when the user has filled in all fields.

So far,

12条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 01:38

    Try utilizing .siblings() , .map() to compile values of form elements , Array.prototype.every() to return Boolean representation of input , textarea values , set disabled property of form input[type=submit] element

    $("form *[required]").on("input change", function(e) {
      $(this).siblings("[type=submit]").prop("disabled"
      , !$(this).siblings(":not([type=submit])").add(this).map(function(_, el) {
        return el.type === "radio" ? el.checked : el.value
       }).get().every(Boolean)
      );
    });
    
    
    Title:
    Description:
    Tag:
    Category: Animation

提交回复
热议问题