【问题标题】:Refreshing a page when user input is wrong用户输入错误时刷新页面
【发布时间】:2021-04-13 20:06:41
【问题描述】:

当用户输入错误时如何重新加载/刷新表单,因为我正在动态生成表单,我设法显示警告消息但在重置字段时遇到问题,还建议我的代码需要任何改进?

...

JavaScript: 
    
    const div = document.querySelector(".addHere");
    
    document.querySelector(".btn").addEventListener("click", addInputs);
    
    function addInputs() {
    
    const inputValue = parseInt(document.querySelector(".inputValue").value);
    
      if (isNaN(inputValue)) {
        alert("Wrong input");

          const form = document.createElement("form");
          form.method = "post";
          form.action = "#";

      } else {
        for (let i = 1; i <= inputValue; i++) {
    
          const input1 = document.createElement("input");
          input1.type = "text";
          input1.maxLength = "12";
          input1.className = "factor";
          input1.required = true;
    
          const input2 = document.createElement("input");
          input2.type = "text";
          input2.maxLength = "1";
          input2.className = "priority";
          input2.required = true;
    
          const br = document.createElement("br");
    
          form.appendChild(br.cloneNode());
          form.appendChild(input1);
          form.appendChild(input2);
          form.appendChild(br.cloneNode());
    
          div.appendChild(form);
        }
    
        const sub = document.createElement("button");
        sub.type = "submit";
        sub.value = "Submit";
        sub.className = "subButton";
        sub.textContent = "Submit";
    
        div.appendChild(sub);
      }
    }

HTML:

<div class="addHere"></div>
<div class="inputs">
  <input type="text" maxlength="1" class="inputValue" placeholder="insert numbers:" />
  <button class="btn">+</button>
</div>

【问题讨论】:

    标签: javascript forms refresh


    【解决方案1】:

    您可以像这样将整个表单的输入重置为其原始值:

    <script>
        document.getElementById('myform').reset();
    </script>
    

    或者只有一个这样的输入元素:

    <script>
        document.getElementById('userName').value = '';
    </script>
    

    如果你想重新加载整个页面,你应该这样做:

    <script>
        document.location.href = document.location.href;
    </script>
    
    <form id="myform">
    
        <input id="userName" type="text" />
    
    </form>
    

    这个怎么样?

    CCS:

        <style>
            .subButton {
                display: none;
            }
        </style>
    

    脚本:

        <script>
            window.addEventListener('load', (event) => {
                document.querySelector(".btnAdd").addEventListener("click", addInputs);
            });
    
            function addInputs() {
                const inputValue = parseInt(document.querySelector(".inputValue").value);
                if (isNaN(inputValue) || inputValue < 1) {
                    alert("Wrong input");
                    document.querySelector(".inputValue").value = '';
                } else {
                    let html = '';
                    for (let i = 1; i <= inputValue; i++) {
                        html += '<input type="text" maxlength="12" class="factor" required>';
                        html += '<input type="text" maxlength="1" class="priority" required>';
                        html += '<br />';
                    }
                    document.querySelector(".addHere").innerHTML = html;
                    document.querySelector(".subButton").style.display = 'block';
                    document.querySelector(".inputs").style.display = 'none';
                }
            }
        </script>
    

    HTML:

    <body>
        <form method="post" action="#">
            <div class="addHere"></div>
            <input type="submit" value="Submit" class="subButton" />
            <div class="inputs">
                <input type="text" maxlength="1" class="inputValue" placeholder="insert numbers:" />
                <button type="button" class="btnAdd">+</button>
            </div>
        </form>
    </body>
    

    【讨论】:

      【解决方案2】:

      试试这一行

      document.getElementById('input').value = "";
      

      【讨论】:

        猜你喜欢
        • 2018-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-25
        • 2017-08-10
        • 1970-01-01
        相关资源
        最近更新 更多