我必须防止在搜索表单上提交空搜索。表单没有提交按钮,所以我必须阻止输入。
html代码:
表格 1
<form method="get" class="search-form" id="searchform" action="http://example.com" >
<input class="text" name="s" id="s" type="text" />
</form>
表格 2
<form action="http://example.com" class="search-form" method="get">
<input type="text" name="s" class="text">
</form>
Javascript 代码
// Im sure this funcions returns the 2 different forms,
var searchForms = getElementsByClass('search-form');
for(i in searchForms)
{
if (searchForms[i].addEventListener)
{
searchForms[i].addEventListener("submit", function(e)
{
preventSubmit(e); // no problem here
console.log(i) // ALWAYS LOGS 1
});
} //I also implemented the ie code, but not necessary here, is the same as above for addEventListener
}
每次我提交任何表单时,都会在控制台中写入 1,有什么想法吗?