2017-04-01 1 views
0
  1. "제출"을 클릭 한 후 페이지에 머물러보십시오.
  2. "컴퓨터 번호"및 "이익"과 같은 입력 데이터는 빈 칸 내에 있습니다.
  3. "제출 됨"이라는 단어가이 페이지의 가운데에 표시됩니다.

다음은 제 코드입니다. 제발 도와주세요, 감사합니다!양식을 제출 한 후 같은 페이지로 이동하여 페이지에 단어를 표시하십시오.

<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>

+1

에 오신 것을 환영합니다! 코드가 어떻게 작동하지 않는지 자세히 설명해 주시겠습니까? 당신은 무엇을 기대하고 있었고 실제로 무슨 일이 일어 났습니까? 예외/오류가있는 경우 발생한 행을 게시하고 예외/오류 세부 정보를 게시하십시오. 이 세부 정보를 입력하거나 편집하지 않으면 도움이되지 않을 수 있습니다. – FrankerZ

답변

0

먼저 양식 이름이 form이므로 document.forms["my 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>

0

안녕하세요 당신이 양식을

을 보내는 당신이 버튼

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

될 수 AJAX를 사용하여 접경 생각해야한다 그리고이 캠은 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; 
} 
+0

함수 이름이 일치하지 않습니다. (또한'evaluate'은 매우 까다로운 이름입니다.) – nnnnnn

관련 문제