<分区>
这是我的 html
代码:
<form name="searchForm" action="javascript:;" method="post" />
<p>
<label for="search">Search the site:</label>
<input type="text" id="search" name="search" value="xhr" />
<input type="submit" value="Search" />
</p>
</form>
在 header 中,我包含了我的 JavaScript 文件和以下特定代码:
window.onload = function () {
getValues = function (id) {
var i, values = [], form = document.getElementById(id);
for (i = 0; i < form.length ;i++) {
values.push( form.elements[i].value );
}
return values;
}
}
我尝试使用 console.log(getValues("searchForm") );
访问该函数,但在我的 Firefox
控制台中,我收到了以下错误: TypeError: form is null
.
谁能告诉我为什么这不起作用?