onkeydown函数返回Uncaught TypeError:无法读取属性'提交'为null

时间:2014-05-09 15:55:53

标签: javascript html forms submit onkeydown

我从JavaScript提交表单时遇到问题。

我使用onkeydown功能,浏览器检测到javascript代码中的错误。错误说:

  

未捕获的TypeError:无法读取属性'提交'为null

我尝试了不同的解决方案,但找不到解决我问题的解决方案。

以下是HTML代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script type="text/javascript" src="default.js"></script>
    </head>
    <body>
        <form id="searchFrom" action="default.php" method="post">
            <table class="menu">
                <tbody class="tbody1">
                    <tr>
                        <td id="searchfield">Sök</td>
                        <td><input id="search" type="text" name="search" ></td>
                    </tr>
                </tbody>
            </table>
        </form>
    </body>
</html>

以下是JavaScript代码:

window.onload = function() {
    document.getElementById("search").onkeydown=function(){
        //setTimeout(function(){submitForm()},3000);
        submitForm()

    };

    function submitForm(){
        document.getElementById("searchForm").submit();

    }

};

2 个答案:

答案 0 :(得分:8)

你有一个错字。
替换

<form id="searchFrom" action="default.php" method="post">
                 ^^

<form id="searchForm" action="default.php" method="post">
                 ^^

答案 1 :(得分:5)

id="searchFrom"更改为id="searchForm"

你有一个简单的错字。