首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一步一步登记表

一步一步登记表
EN

Stack Overflow用户
提问于 2020-05-13 10:59:35
回答 2查看 89关注 0票数 0

我的html中有这样的代码,当系统检测到空时,它将验证input=type --在接下来的步骤中,我将如何禁用它?因为我的一些领域不是必需的。

例如,需要输入类型< input placeholder="Firstname" oninput="this.className = ''" name="MotherFirstname">,而不需要< input placeholder="Middlename" oninput="this.className = ''" name="MotherMiddlename">。如果用户填写的是名字,而不是中间名,那么它将继续下一步,

代码语言:javascript
复制
<tr>
    <td>
        <label for="landmark-formbuilder-0" class="form-control-label mbr-fonts-style display-7"><strong>Contact number *</strong></label>
         <input id="input" placeholder="Firstname" oninput="this.className = ''" name="MotherFirstname">
    </td>
    <td>
        <label for="landmark-formbuilder-0" class="form-control-label mbr-fonts-style display-7"><strong>Middlename *</strong></label>
        <input placeholder="Middlename" oninput="this.className = ''" name="MotherMiddlename">
    </td>
    <td>
        <label for="landmark-formbuilder-0" class="form-control-label mbr-fonts-style display-7"><strong>Lastname *</strong></label>
        <input placeholder="Lastname" oninput="this.className = ''" name="MotherLastname">
    </td>
</tr>
<script>
    function validateForm() {
      // This function deals with validation of the form fields
      var x, y, i, valid = true;
      x = document.getElementsByClassName("tab");
      y = x[currentTab].getElementsByTagName("#input");

      // A loop that checks every input field in the current tab:
      for (i = 0; i < y.length; i++) {
        // If a field is empty...

        if (y[i].value == "") {
          // add an "invalid" class to the field:
          y[i].className += " invalid";
          // and set the current valid status to false
          valid = false;
        }
      }

      // If the valid status is true, mark the step as finished and valid:

      if (valid) {
        document.getElementsByClassName("step")[currentTab].className += " finish";
      }
      return valid; // return the valid status
    }
    </script>

这是我的web控制台中的错误:

从这一行:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-05-13 11:04:40

Javascript不需要#或点通道

代码语言:javascript
复制
y = x[currentTab].getElementsByTagName("#input");

代码语言:javascript
复制
y = x[currentTab].getElementsByTagName("input");

同样在您的图像中,如果您将id设置为输入,则为getElementById("input")

票数 1
EN

Stack Overflow用户

发布于 2020-05-13 11:06:53

您可以在html标记中使用required属性,它将是自动要求的,您可以将不需要的属性保留为空。

参见此JSFiddle:https://jsfiddle.net/hbjrya45/2/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61772863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档