Force user to fill all fields before enabling form submit

前端 未结 12 2051
忘掉有多难
忘掉有多难 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条回答
  •  醉话见心
    2021-02-07 01:52

        var inputs = $("form#myForm input, form#myForm textarea");
    
        var validateInputs = function validateInputs(inputs) {
          var validForm = true;
          inputs.each(function(index) {
            var input = $(this);
            if (!input.val() || (input.type === "radio" && !input.is(':checked'))) {
              $("#subnewtide").attr("disabled", "disabled");
              validForm = false;
            }
          });
          return validForm;
        }
    
    
        inputs.each(function() {
          var input = $(this);
          if (input.type === "radio") {
            input.change(function() {
              if (validateInputs(inputs)) {
                $("#subnewtide").removeAttr("disabled");
              }
            });
          } else {
            input.keyup(function() {
              if (validateInputs(inputs)) {
                $("#subnewtide").removeAttr("disabled");
              }
            });
          }
        });
    
    
    Title:
    Description:
    Tag:
    Category: Animation

提交回复
热议问题