想要添加一个提交按钮而不是Enter键

时间:2020-10-20 05:35:18

标签: javascript html

我想添加一个Submit按钮而不是Enter键

这是我的代码

<!DOCTYPE html>
<html> 
    <body>
        <input type="text" name="logsarchives" id="logsarchives" autocomplete="off"  placeholder="Enter Console Command" onkeydown="redirect(this)">
    </body>
    <script>
        function redirect(ele) {
            if(event.key === 'Enter') {
                if(ele.value === 'google')
                     window.location.href = 'https://google.com';
                else
                    alert("Invalid");
            }
        }
    </script>
</html>

1 个答案:

答案 0 :(得分:0)

onkeydown="redirect(this)"中删除<input>,然后添加提交按钮和包装表单:

<form method="post" action="https://where.you.want/to/process">
    <input type="text" name="logsarchives" id="logsarchives" autocomplete="off"  placeholder="Enter Console Command">
    <input type="submit" name="submit" value="submit>
</form>