比较html文本输入并使用Javascript重定向到另一个页面

Comparing html text inputs and redirecting to another page using Javascript

本文关键字:重定向 另一个 Javascript 文本 html 输入 比较      更新时间:2023-09-26

我是全新的Javascript,我只是用它来做一个简单的网站的乐趣。我试着在网上搜索,但我仍然卡住了,所以如果你能帮助我或重定向我对其他帮助,那将是伟大的。我试图使用Javascript发送用户到另一个html页面在我的网站,如果他们的输入符合我的标准。所以我想用if/else语句来做这件事:如果文本输入等于ODQHVHMJKD,它将把它们发送到page3.html。然而,当我在浏览器上尝试这个时,什么也没有发生——它只是把我带到地址末尾有?codebox1=f&button1=Submit的相同页面。下面是我的脚本部分:

<script type="text/javascript">
function testResults (form) {
    if (form.codebox1.value == ODQHVHMJKD) {
    window.location.pathname = 'page3.html';
    }
    else {
        window.alert("Try again!");
    }  
};
</script>

下面是我的表单元素:

<form name="form1" method="GET">
<input name="codebox1" type="text" />
<input name="button1" type="submit" value="Submit" onClick="testResults(this.form)"/>
</form>

你能帮我一下,这样我就能把它弄出来了吗?很有可能我做的每件事都是完全错误的——任何帮助都是感激的!

试试这个,

function testResults (form) {
    if (form.codebox1.value == "ODQHVHMJKD") {
        window.location = 'page3.html';
    }
    else {
        window.alert("Try again!");
    }  
    return false;
};

您需要阻止表单的默认操作。在提交事件中,调用e.preventDefault();return false。此外,需要在ODQHVHMJKD

周围加上引号。

Js Fiddle: http://jsfiddle.net/prankol57/Ht45t/

也许这能帮到你。

    <form name="form" onsubmit="Results()">
    <input type="text" name="fname" id="val">
    <input type="submit" value="Submit">
    </form>
    <script type="text/javascript">
        function Results() {
          var val = document.getElementById('val').value;
          if (val == "ODQHVHMJKD") {
              window.location = 'page3.html';
           } else {
              window.alert("Try again!");
           }  
        };
    </script>