Javascript preventDefault无效,说事件未定义

时间:2017-01-29 21:55:37

标签: javascript html input

我的代码是这样的:

        var search = document.getElementById("search");
        var searchForm = document.getElementById("searchForm");

        searchForm.onSubmit = function(event) {
            event.preventDefault();
            console.log("BLBLBL");
            var contenders = [];
            var searchValue = search.value;
            for (var i = 0; i < cards.length; i++) {
                for (var j = 0; j < searchValue.length; j++) {
                    if (searchValue[j] === cards[i][j]) {
                        contenders.push(cards[i]);
                    } else {
                        delete cards[i];
                        break;
                    }
                }
            }
            console.log(contenders);
        }

在包含此表单的HTML文件中:

<form id="searchForm">
        <input id="search"></input>
</form>

当我在HTML页面中运行代码时,它会说:

buyPokemonCardSearch.html:30 Uncaught TypeError: Cannot read property 'preventDefault' of undefined
at HTMLFormElement.searchForm.onSubmit (buyPokemonCardSearch.html:30)
at <anonymous>:1:12

[这是假的网站,所以不要判断] 为什么不起作用? console.log是在那里因为我想检测我的代码出错的地方。

0 个答案:

没有答案