我正在尝试构建一个小搜索脚本,但似乎无法在您点击提交时阻止页面重新加载。我的代码可能有问题:
function searchThat(e) {
if (!e) e = window.event;
e.preventDefault();
if (searchFor.indexOf(searchIn) != -1) {
console.log('we found it');
}
}
非常感谢!
我用它来称呼它
if (searchFor && searchIn && searchSubmit) {
addEvent(searchSubmit, 'submit', searchThat)
}
和:
function addEvent(elem, evtType, func) {
if (elem.addEventListener) {
elem.addEventListener(evtType, func, false);
} else if (elem.attachEvent) {
elem.attachEvent("on" + evtType, func);
} else {
elem["on" + evtType] = func;
}
}
和 HTML:
<div class="container">
<form>
<p>Search for:</p>
<p class="mrm">
<input type="text" id="searchFor" name="searchFor">
</p>
<p>In:</p>
<textarea class="mrm" type="text" id="searchIn" name="searchIn"></textarea>
<p>
<input type="submit" class="clicker nmb" id="searchSubmit" name="searchSubmit">
</p>
</form>
</div>