Javascript 在HTML Java脚本中尝试了几次错误之后,如何显示我的答案 请帮我添加一个名为“查看答案”的“按钮” 此“查看您的答案”按钮仅在用户单击“检查您的答案”按钮3次后出现,并且有3个答案是错误的 单击“查看您的答案”按钮后,将显示正确答案 正确答案是“100”
谢谢你Javascript 在HTML Java脚本中尝试了几次错误之后,如何显示我的答案 请帮我添加一个名为“查看答案”的“按钮” 此“查看您的答案”按钮仅在用户单击“检查您的答案”按钮3次后出现,并且有3个答案是错误的 单击“查看您的答案”按钮后,将显示正确答案 正确答案是“100”,javascript,html,Javascript,Html,谢谢你 你的利润是多少? 检查你的答案 函数myFunction(){ 变量x,文本; x=document.getElementById(“id1”).value; 如果(isNaN(x)| | x!=100){ text=“不正确”document.getElementById(“Q1”).style.color=“红色”; }否则{ text=“Correct”document.getElementById(“Q1”).style.color=“绿色”; } document.get
你的利润是多少?
检查你的答案
函数myFunction(){
变量x,文本;
x=document.getElementById(“id1”).value;
如果(isNaN(x)| | x!=100){
text=“不正确”document.getElementById(“Q1”).style.color=“红色”;
}否则{
text=“Correct”document.getElementById(“Q1”).style.color=“绿色”;
}
document.getElementById(“Q1”).innerHTML=text;
}
基本上,您需要的是一点应用程序状态。您需要在JavaScript中使用一个变量,其行为numberOfSubmissions
。如果答案不正确,请单击的处理程序,检查您的答案
incrementnumberofincorrectedsubmissions
。递增后,检查该值是否为3或更高。如果是,则显示您的查看答案按钮
如果有任何不清楚的地方,或者您在实施过程中遇到困难,请告诉我。这就是想法。试试这个。更改变量名以及其他不能更改的内容
您需要一个全局变量来存储本例中的“尝试次数”
每次失败时,Y增加1
函数上还有一个if语句,如果fail等于“尝试次数”的大约,它将做一些事情。您可以让它执行任何您想要的操作,包括重置尝试计数器
干杯
<head>
<title> </title>
</head>
<body>
<p>How much is your profit?
<input id="id1" name = "id1" required>
<button type = "button" onclick="myFunction()">Check Your Answer</button> </p>
<p id="Q1"></p>
<p id="Q2"></p>
<script>
var y=0;
function myFunction() {
var x, text, text2;
x = document.getElementById("id1").value;
if (isNaN(x) || x != 100) {
text = "Incorrect";
document.getElementById("Q1").style.color = "red";
y=y+1;
}
else {
text = "Correct";
document.getElementById("Q1").style.color = "green";
}
if (y==3){
text2 = "DONE";
document.getElementById("Q2").style.color = "orange";
}
document.getElementById("Q1").innerHTML = text;
document.getElementById("Q2").innerHTML = y;
}
</script>
</body>
</html>
你的利润是多少?
检查你的答案
var y=0;
函数myFunction(){
变量x,text,text2;
x=document.getElementById(“id1”).value;
如果(isNaN(x)| | x!=100){
text=“不正确”;
document.getElementById(“Q1”).style.color=“红色”;
y=y+1;
}
否则{
text=“正确”;
document.getElementById(“Q1”).style.color=“绿色”;
}
如果(y==3){
text2=“完成”;
document.getElementById(“Q2”).style.color=“橙色”;
}
document.getElementById(“Q1”).innerHTML=text;
document.getElementById(“Q2”).innerHTML=y;
}
您可以添加一个全局变量来跟踪错误答案的数量。我不能说出全局变量不受欢迎的所有原因,但基本上它们会扰乱全局名称空间,并可能导致冲突,诸如此类的事情。不过我希望这能有所帮助
<html>
<head>
<title> </title>
</head>
<body>
<p>How much is your profit?
<input id="id1" name = "id1" required>
<button type = "button" onclick="myFunction()">Check Your Answer</button> </p>
<p id="Q1"></p>
<script>
var failedAttempts = 0;
function myFunction() {
var x, text;
x = document.getElementById("id1").value;
if (failedAttempts === 3){
// Show the answer
}else if(isNaN(x) || x != 100) {
text = "Incorrect"; document.getElementById("Q1").style.color = "red";
failedAttempts++;
} else {
text = "Correct"; document.getElementById("Q1").style.color = "green";
}
document.getElementById("Q1").innerHTML = text;
}
</script>
</body>
</html>
你的利润是多少?
检查你的答案
var failedAttempts=0;
函数myFunction(){
变量x,文本;
x=document.getElementById(“id1”).value;
如果(失败尝试===3){
//给出答案
}else如果(isNaN(x)| x!=100){
text=“不正确”document.getElementById(“Q1”).style.color=“红色”;
失败尝试++;
}否则{
text=“Correct”document.getElementById(“Q1”).style.color=“绿色”;
}
document.getElementById(“Q1”).innerHTML=text;
}
您好,
看看它是否是您所需要的:
我将一个全局变量用作计数器。
我按下按钮查看答案。
<html>
<head>
<title> </title>
</head>
<body>
<p>How much is your profit?
<input id="id1" name = "id1" required>
<button type="button" onclick="myFunction()">Check Your Answer</button> </p>
<button type="button" id="btn2" onclick="seeYourAnswer()" style="display:none;">See your answer</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('btn2').style.display = 'block';
document.getElementById("Q1").innerHTML = '';
} else {
document.getElementById('btn2').style.display = 'none';
}
}
function seeYourAnswer(){
text = "the correct answer is '100'."; document.getElementById("Q1").style.color = "red";
document.getElementById("Q1").innerHTML = text;
}
</script>
</body>
</html>
你的利润是多少?
检查你的答案
看到你的答案了吗
var-erroscont=0;
函数myFunction(){
变量x,文本;
x=document.getElementById(“id1”).value;
如果(isNaN(x)| | x!=100){
text=“不正确”document.getElementById(“Q1”).style.color=“红色”;
ERROSCONT++;
}否则{
text=“Correct”document.getElementById(“Q1”).style.color=“绿色”;
}
document.getElementById(“Q1”).innerHTML=text;
如果(ERROSCONT==3){
ErroScont=0;
document.getElementById('btn2')。style.display='block';
document.getElementById(“Q1”).innerHTML='';
}否则{
document.getElementById('btn2').style.display='none';
}
}
函数seeYourAnswer(){
text=“正确答案是‘100’”;document.getElementById(“Q1”).style.color=“红色”;
document.getElementById(“Q1”).innerHTML=text;
}
我希望这对你有帮助