如何在 JavaScript 中执行重定向操作,如果某个条件发生?

huangapple go评论76阅读模式
英文:

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 == &quot;Vonsild&quot;) {
    location.href = &quot;/lykkelig.htm&quot;
  } else {
    location.href = &quot;&quot;
  }
  document.getElementById(&#39;knap&#39;).src = enter;
  document.getElementById(&#39;tekst&#39;).src = bynavn;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
  &lt;meta name=&quot;robots&quot; content=&quot;noindex, nofollow&quot;&gt;
  &lt;meta name=&quot;googlebot&quot; content=&quot;noindex, nofollow&quot;&gt;
  &lt;title&gt;Niveau 5&lt;/title&gt;
  &lt;link href=&quot;/ZPWSODwhnzO!SJKAzmaPQ/style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;div&gt;
    &lt;img src=&quot;hvorervihenne.png&quot; width=&quot;1199&quot; height=&quot;611&quot; /&gt;
    &lt;br&gt; Hvor er vi?: &lt;input type=&quot;text&quot; id=&quot;tekst&quot;&gt;
    &lt;button onclick id=&quot;knap&quot;&gt;Enter&lt;/button&gt;
    &lt;script src=&quot;/svar.js&quot;&gt;&lt;/script&gt;
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;noindex, nofollow&quot;&gt;
    &lt;meta name=&quot;googlebot&quot; content=&quot;noindex, nofollow&quot;&gt;
    &lt;title&gt;Niveau 5&lt;/title&gt;
    &lt;link href=&quot;/ZPWSODwhnzO!SJKAzmaPQ/style.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div&gt;
      &lt;img src=&quot;hvorervihenne.png&quot; width=&quot;1199&quot; height=&quot;611&quot;/&gt;
      &lt;br&gt;
      Hvor er vi?: &lt;input type=&quot;text&quot; id=&quot;tekst&quot;&gt;
      &lt;button onclick=&quot;checkAnswer()&quot;&gt;Enter&lt;/button&gt;
      &lt;script src=&quot;/svar.js&quot;&gt;&lt;/script&gt;
    &lt;/div&gt;
    &lt;script&gt;
      function checkAnswer() {
        var bynavn = document.getElementById(&#39;tekst&#39;).value;
        
        if (bynavn.toLowerCase() === &quot;vonsild&quot;) {
          window.location.href = &quot;/lykkelig.htm&quot;;
        } else {
          window.location.href = &quot;&quot;; // Provide the URL you want to redirect to on incorrect answer
        }
      }
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年6月25日 19:46:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定