我的代码是这样的:
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是在那里因为我想检测我的代码出错的地方。