簡體   English   中英

提交表格后,留在同一頁面,在頁面中顯示一個單詞

[英]After submit a form, stay same page, show a word in the page

  1. 單擊“提交”后,留在頁面上。
  2. 輸入數據(例如“計算機號”和“利潤”)留在這些空白方框內。
  3. 此頁面的中心出現一個單詞“ Submitted”。

以下是我的代碼,請幫忙,謝謝!

 <html> <head> <title></title> </head> <body> <form name="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()">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> 

您好,您應該在發送表單時考慮使用AJAX

這可以是按鈕

<button type="button"
onclick="validateForm('ajax_info.php', myFunction)"> Clic to Submit
</button>

這個凸輪就是AJAX功能

  function validateForm(url, cFunction) {
  var xhttp;
  xhttp=new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      cFunction(this);
    }
  };
  xhttp.open("GET", url, true); //can be POST
  xhttp.send();
}
function myFunction(xhttp) {
  document.getElementById("submited").innerHTML =
  xhttp.responseText;
} 

首先,您的document.forms["my form"]無效,因為您的表單名稱是form所以我將其更改為document.forms["form"]

並且,在提交時,我在函數的底部添加了return false以保留在頁面上。 另外,在此之前,請在頁面中央添加“已提交”文本,如下所示。

這是工作代碼段!

希望有幫助!

 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; } function validateForm() { var q = document.forms["form"]["Computer"].value; if (q == "") { alert("Computer Number is Missing!"); return false; } var w = document.forms["form"]["id1"].value; if (w != "100") { alert("Question 1 is Incorrect!"); return false; } document.getElementById("submitted").innerHTML = "Submitted" return false; } 
 #submitted { text-align: center } 
 <form name="form" onsubmit="return validateForm()"> Computer Number:<br> <input type="text" name="Computer"><br> <p>How much is your profit? <input id="id1" name="id1" required> <button type="button" onclick="myFunction()">My Answer</button> <button type="button" id="btn1" onclick="Solution()" style="display:none;">Solution</button> </p> <p id="Q1"></p> <input type="submit" value="Submit"> </form> <br/> <div id="submitted"> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM