英文:
If some condition happens, how to execute redirect actions in javascript?
问题
I can provide a translation of the non-code parts for you:
"I'm very new to JavaScript but have worked on HTML from time to time.
I've made a text box with a button 'Enter' to enter the answer, the problem is I don't know how to really make the script (since I'm very new to JS). I want to know how to redirect the user to a new page if the answer is correct from the text box where they input.
I am also new to this website, so I might not know what to do here to start off with.
Here's the code:
Help would be very much appreciated.
I've tried looking on this website, looking after my issue, but it was in a different coding language, and I didn't want to do that."
英文:
I'm very new to JavaScript but have worked on HTML from time to time.
I've made a text box with a button "Enter" to enter the answer, the problem is I don't know how to really make the script (since I'm very new to JS). I want to know how to redirect the user to a new page, if
the answer is correct from the text box where they input.
I am also new to this website, so I might not know what to do here to start off with.
Here's the code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function submit() {
var bynavn;
if (bynavn == "Vonsild") {
location.href = "/lykkelig.htm"
} else {
location.href = ""
}
document.getElementById('knap').src = enter;
document.getElementById('tekst').src = bynavn;
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<title>Niveau 5</title>
<link href="/ZPWSODwhnzO!SJKAzmaPQ/style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div>
<img src="hvorervihenne.png" width="1199" height="611" />
<br> Hvor er vi?: <input type="text" id="tekst">
<button onclick id="knap">Enter</button>
<script src="/svar.js"></script>
</div>
</body>
</html>
<!-- end snippet -->
Help would be very much appreciated.
I've tried looking on this website, looking after my issue, but it was in a different coding language and I didn't want to do that.
答案1
得分: 0
以下是您提供的代码的翻译部分:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<title>Niveau 5</title>
<link href="/ZPWSODwhnzO!SJKAzmaPQ/style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div>
<img src="hvorervihenne.png" width="1199" height="611"/>
<br>
Hvor er vi?: <input type="text" id="tekst">
<button onclick="checkAnswer()">Enter</button>
<script src="/svar.js"></script>
</div>
<script>
function checkAnswer() {
var bynavn = document.getElementById('tekst').value;
if (bynavn.toLowerCase() === "vonsild") {
window.location.href = "/lykkelig.htm";
} else {
window.location.href = ""; // 在回答不正确时提供您要重定向到的 URL
}
}
</script>
</body>
</html>
请注意,代码部分未被翻译,只返回了您提供的 HTML 代码的原始内容。
英文:
Try this code :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<title>Niveau 5</title>
<link href="/ZPWSODwhnzO!SJKAzmaPQ/style.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div>
<img src="hvorervihenne.png" width="1199" height="611"/>
<br>
Hvor er vi?: <input type="text" id="tekst">
<button onclick="checkAnswer()">Enter</button>
<script src="/svar.js"></script>
</div>
<script>
function checkAnswer() {
var bynavn = document.getElementById('tekst').value;
if (bynavn.toLowerCase() === "vonsild") {
window.location.href = "/lykkelig.htm";
} else {
window.location.href = ""; // Provide the URL you want to redirect to on incorrect answer
}
}
</script>
</body>
</html>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论