【发布时间】:2020-07-10 22:01:57
【问题描述】:
我已经尝试了下面的代码,但是当我按下回车键时,没有任何反应(它应该执行搜索功能)。
<head>
<script type="text/javascript">
$(document).ready(function() {
prepareSearch($('#searchBox'), {
'searchLine': '.searchTarget div.product',
'searchSightPoint': 'div.stand, span.apron, div.aircraft, div.image',
'fade': true,
'contrastString': false
});
$('#standsearch').submit(function(e) {
e.preventDefault();
});
});
var input = document.getElementById("searchBox");
input.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("btn").click();
}
});
</script>
</head>
<body>
<div class="row" id="standsearch">
<form class="form-search" action="#standsearch">
<div class="input-append">
<input type="text" id="searchBox" class="span2 search-query" placeholder="Search Stands" />
<button type="button" id="btn" class="btnsearch">Find</button>
</div>
</form>
</div>
</body>
我尝试过使用 onclick="" 但是我不确定我应该在其中添加什么。
【问题讨论】:
-
您没有 id 为 'standardsearch' 的元素,首先将
$('#standsearch')替换为$('.form-search').以通过其类名访问表单。 -
在哪里捕捉到 id btn 元素的点击事件?
-
@Triby 这样做会导致搜索功能完全停止工作。另外,我确实有一个 id 为“standsearch”的元素(第一个 div 元素)。
-
div没有submit方法:$('#standsearch').submit(function(e) { -
我现在该怎么办?我已经按照你的建议做了,但没有任何改变,我仍然遇到同样的问题。 @Triby
标签: javascript html search onclick