单击“提交”按钮后如何保持同一页面,并保留我在JavaScript中输入的日期

时间:2017-03-31 16:51:53

标签: javascript html

  1. 点击“提交”后,请停留在此页面上。
  2. 输入数据,如“电脑号码”和“利润”,留在那些空白的广场内。
  3. “已提交”一词出现在此页面的中央。
  4. 以下是我的代码, 请帮忙,谢谢!

    <html>
    <head>
    <title></title>
    </head>
    
      <body>
    
      <form name="my form" 
    onsubmit="return validateForm()">
       
        Computer Number:<br>
        <input type="text" name="Computer" required><br>
      
    <p>How much is your profit?                                 
    
    <input id="id1" name = "id1" required>
      <button  type = "button" onclick="myFunction()">Check My Answer</button> 
        <button type="button" id="btn1" onclick="Solution()" style="display:none;">Solution</button>
        </p>
    
    <p id="Q1"></p>
    
    <script>
    var errosCount = 0;
      function myFunction() {
        var x, text;
        x = document.getElementById("id1").value;
       
        if (isNaN(x) || x != 100) {
            text = "Incorrect"; document.getElementById("Q1").style.color = "red";errosCount++;
        } else {
            text = "Correct"; document.getElementById("Q1").style.color = "green";
        }
        document.getElementById("Q1").innerHTML = text;
             if(errosCount === 3){
            errosCount = 0;
            document.getElementById('btn1').style.display = 'block';
            document.getElementById("Q1").innerHTML = '';
          } else {
            document.getElementById('btn1').style.display = 'none';        
          } 
    }
    function Solution(){
      text = "(P - w) * q<sub>o</sub> - I = (53 - 43) * 30 - 200 = 100";  document.getElementById("Q1").style.color = "red";
      document.getElementById("Q1").innerHTML = text;
    }      
    </script>
     
      <input type="submit" value="Submit">
      
    </form>
      
      <script>
    function validateForm() {
        var q = document.forms["my form"]["Computer"].value;
        if (q == "") {
            alert("Computer Number is Missing!");
            return false;}
      var w = document.forms["my form"]["id1"].value;
        if (w != "100") {
            alert("Question 1 is Incorrect!");
            return false;}
      
    }
    </script>
       
    </body>
    </html>

1 个答案:

答案 0 :(得分:2)

validateForm()中,我添加了else逻辑,用于显示提交的邮件并停止重定向表单。

要做的事情。

根据您的要求添加逻辑如何通过XMLHttpRequest方式提交表单。有关详情,请参阅Sending forms through JavaScript

&#13;
&#13;
<html>

<head>
  <title></title>
</head>

<body>

  <form name="my form" onsubmit="return validateForm()">

    Computer Number:<br>
    <input type="text" name="Computer" required><br>

    <p>How much is your profit?

      <input id="id1" name="id1" required>
      <button type="button" onclick="myFunction()">Check My Answer</button>
      <button type="button" id="btn1" onclick="Solution()" style="display:none;">Solution</button>
    </p>

    <p id="Q1"></p>

    <script>
      var errosCount = 0;

      function myFunction() {
        var x, text;
        x = document.getElementById("id1").value;

        if (isNaN(x) || x != 100) {
          text = "Incorrect";
          document.getElementById("Q1").style.color = "red";
          errosCount++;
        } else {
          text = "Correct";
          document.getElementById("Q1").style.color = "green";
        }
        document.getElementById("Q1").innerHTML = text;
        if (errosCount === 3) {
          errosCount = 0;
          document.getElementById('btn1').style.display = 'block';
          document.getElementById("Q1").innerHTML = '';
        } else {
          document.getElementById('btn1').style.display = 'none';
        }
      }

      function Solution() {
        text = "(P - w) * q<sub>o</sub> - I = (53 - 43) * 30 - 200 = 100";
        document.getElementById("Q1").style.color = "red";
        document.getElementById("Q1").innerHTML = text;
      }
    </script>

    <input type="submit" value="Submit">
    <span id="success"></span>

  </form>

  <script>
    function validateForm() {
      var q = document.forms["my form"]["Computer"].value;
      if (q == "") {
        alert("Computer Number is Missing!");
        return false;
      }
      var w = document.forms["my form"]["id1"].value;
      if (w != "100") {
        alert("Question 1 is Incorrect!");
        return false;
      } else {
        /* ajax logic here and on success message this message is followed*/
        document.getElementById('success').innerHTML = "<b>Submitted</b>"
        return false; /*stops the form from being submitted normal way*/
      }

    }
  </script>

</body>

</html>
&#13;
&#13;
&#13;