强制用户在启用表单提交之前填写所有字段 [英] Force user to fill all fields before enabling form submit

查看:68
本文介绍了强制用户在启用表单提交之前填写所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含各种字段的表格.

I have a form containing various fields.

请参见 jsFiddle演示 .

我的目标是仅在用户填写所有字段时启用提交按钮.

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

到目前为止,在启用提交"按钮之前,我可以强制标题字段具有内容.我如何做到使所有其他字段也都必须在启用之前启用.

So far, I'm able to force the title field to have content before submit button is enabled. How do I make it so that all other fields need to be filled too before submit button is enabled.

    jQuery("input[type='text']").on("keyup", function () {
        if (jQuery(this).val() != "" ) {
            if (jQuery("#titlenewtide").val() != '')
            {
                jQuery("#subnewtide").removeAttr("disabled");
            }
        } else {
            jQuery("#subnewtide").attr("disabled", "disabled");
        }
    });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#" method="post" id="new_tide">
        Title: <input id="titlenewtide" type="text" name="title" required> <br>
        Description: <textarea name="description" id="description"></textarea> <br>
        Tag:  <input id="newtag" type="text" name="newtag" required> <br>
        Category: <input type="radio" name="category" value="19" required> Animation
        <button type="submit" value="Submit" name="subnewtide" id="subnewtide" disabled="disabled">Submit</button> 
    </form>

请注意,我正在将JavaScript加载到页脚中.

Note that I am loading the JavaScripts in my footer.

推荐答案

  • 使更改在输入值更改后生效:
  • 在每个input更改中,测试其他inputs的值并检查radio的状态,如果已输入所有inputs,则将启用提交button:

    On each input change, test the values of other inputs and checked state of radio, if all inputs has been entered it will make the submit button enabled:

    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.change(function() {
      if (validateInputs(inputs)) {
        $("#subnewtide").removeAttr("disabled");
      }
    });
    

    演示:

    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.change(function() {
      if (validateInputs(inputs)) {
        $("#subnewtide").removeAttr("disabled");
      }
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form action="#" method="post" id="myForm">
    
      Title:
      <input id="titlenewtide" type="text" name="title" required>
      <br>Description:
      <textarea name="description" id="description"></textarea>
      <br>Tag:
      <input id="newtag" type="text" name="newtag" required>
      <br>Category:
      <input type="radio" name="category" value="19" required>Animation
      <button type="submit" value="Submit" name="subnewtide" id="subnewtide" disabled="disabled">Submit</button>
    
    </form>

    它也使用形式id="myForm",因此您可以使用它来仅验证页面中的特定形式.

    Also it uses the form id="myForm", so you can use it to validate only specific forms in your pages.

    注意:这已经过测试,可以在ChromeFirefoxIE上使用.

    Note: This is tested and working on Chrome, Firefox and IE.

    • 当我们输入以下内容时,更改才会生效:

    在先前的代码中,我们使用onchange事件处理程序来调用该函数,因此仅当我们在给定输入之外单击(更改后)时才调用该函数.

    In the previous code we are using onchange event handler to call the function so it's only called when we click outside a given input (after change).

    要在用户在字段(最后一个)中输入字符时自动执行呼叫,我们需要使用onkeyup事件,因此我们无需在事件外单击即可.

    To perform the call automatically when the user enters a character in a field (the last one) we need to use the onkeyup event so we don't need to click outside of it.

    这是您需要更改的代码:

    This is the changed code you need :

        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");
              }
            });
          }
        });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <form action="#" method="post" id="myForm">
    
      Title:
      <input id="titlenewtide" type="text" name="title" required>
      <br>Description:
      <textarea name="description" id="description"></textarea>
      <br>Tag:
      <input id="newtag" type="text" name="newtag" required>
      <br>Category:
      <input type="radio" name="category" value="19" required>Animation
      <button type="submit" value="Submit" name="subnewtide" id="subnewtide" disabled="disabled">Submit</button>
    
    </form>

    这篇关于强制用户在启用表单提交之前填写所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆