我正在使用jquery在我的网站上使用搜索栏执行实时自动建议。但我有一个问题,除了jquery示例信息之外,如何在该字段中没有任何内容时停止显示自动建议框
//////带有Jquery的HTML表单
<div id="searchbox_largesearcher_bg">
<div id="searchbox_signalbg"></div>
<form id="large-searcher" action="" method="post">
<input name="large-search-form" id="large-search-form" type="text" size="35" onfocus="if (this.value == 'Search A Genre e.g. "Hip-Hop"') this.value = '';" onblur="if (this.value == '') this.value = 'Search A Genre e.g. "Hip-Hop"';" maxlength="35" value="Search A Genre e.g. "Hip-Hop""/>
<input name="login" id="search_btn" class="search" type="submit" value="" />
<div id="searchbox_searchicn"></div>
</form>
</div>
<div class="dropdown">
<ul class="results"></ul>
</div>
////// CSS
.dropdown {
-moz-border-bottom-colors: none;
-moz-border-image: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #F5F5F5;
border-radius: 5px 5px 5px 5px;
box-shadow: 0 1px 2px -1px #000000;
margin-left: 32px;
margin-top: -12px;
padding: 0;
position: absolute;
width: 460px;
z-index: 99;
}
.results {
border: 1px solid #A2A1A1;
border-radius: 5px 5px 5px 5px;
list-style: none outside none;
padding-bottom: 7px;
padding-top: 7px;
width: 458px;
}
.results li{
border-bottom: 1px solid #EDEDED;
color: #A8A8A8;
cursor: pointer;
font-family: helvetica neue,helvetica,arial sans-serif;
margin-left: 20px;
margin-right: 20px;
padding-bottom: 14px;
padding-left: 25px;
padding-top: 10px;
}
.results li:hover{
background: none repeat scroll 0 0 #EEEEEE;
color: #A8A8A8;
}
//////// Jquery for auto suggest
<script type="text/javascript">
// Large Index Page Live Returns Searcher
$(document).ready(function(){
$('#large-search-form').keyup(function() {
var search_term = $(this) .attr('value');
$.post('inc/resultsgrabber.php', {search_term:search_term}, function (data) {
$('.results').html(data);
$('.results li').click(function() {
var result_value = $ (this) .text();
$('#large-search-form').attr('value', result_value);
$('.results').html('');
});
});
});
});
答案 0 :(得分:1)
最好的方法是在ajax调用后检查data
响应中是否有任何数据。
.results { display:none }
$.post('inc/resultsgrabber.php', {search_term:search_term}, function (data) {
if (data == '') {
$('.results').html(data);
.
.
.
.
.
}
});
答案 1 :(得分:0)
原来是一个CSS问题,我在一些错误的地方填充和边框。 jquery代码完全没问题。抱歉。