我创建了一个自动通过数据库记录建议的搜索框,每当我搜索或输入我的搜索框重复的内容时。如何删除我的搜索框的重复?
<script>
$(document).ready(function(){
$("#search_list").keyup(function(){
$.ajax({
type: "POST",
url: "index.php",
data:"keyword="+$(this).val(),
success: function(data){
$("#suggestion").show();
$("#suggestion").html(data);
$("#search_list").css("background","#FFF");
}
});
});
});
//To select name
function selectName(val) {
$("#search_list").val(val);
$("#suggestion").hide();
}
</script>
-------------- PHP --------------
<?php
foreach($result as $name) {
?>
<div class="list">
<a href="#" class="style_suggestions" onClick="selectName('<?php echo $name["n_Name"]; ?>');"><?php echo $name["n_Name"]; ?></a>
</div>
<?php } ?>
<?php } } ?>
------------- HTML ------------------
<html>
<head>
<title>Welcome to Nava</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible content=" ie="edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css" type="text/css">
<script type="text/javascript"></script>
<script src="jquery.min.js"></script>
</head>
<body>
<div class="frmSearch">
<input type="text" class="form-control" id="search_list" name="search_nv_box" placeholder="Name"/>
<div id="suggestion"></div>
</div>
答案 0 :(得分:0)
您必须清除<div id="suggestion"></div>
内容只需将此行添加到您的成功中:$("#suggestion").html('');
success: function(data){
$("#suggestion").html(''); // clear your data here
$("#suggestion").show();
$("#suggestion").html(data);
$("#search_list").css("background","#FFF");
}
答案 1 :(得分:0)
试试这个,
首先,您需要清除或清空现有建议并在此之后加载新数据。
$("#suggestion").empty();
$("#suggestion").html(data);
应该是,
$(document).ready(function(){
$("#search_list").keyup(function(){
$.ajax({
type: "POST",
url: "index.php",
data:"keyword="+$(this).val(),
success: function(data){
$("#suggestion").empty();
$("#suggestion").html(data);
$("#suggestion").show();
$("#search_list").css("background","#FFF");
}
});
});
});
此外,您应该只从php返回单个div元素。目前似乎每个记录都会创建一个div。
<?php
foreach($result as $name) {
?>
<div class="list">
<a href="#" class="style_suggestions" onClick="selectName('<?php echo $name["n_Name"]; ?>');"><?php echo $name["n_Name"]; ?></a>
</div>
<?php } ?>
应该是,
<div class="list">
<?php
foreach($result as $name) {
?>
<a href="#" class="style_suggestions" onClick="selectName('<?php echo $name["n_Name"]; ?>');"><?php echo $name["n_Name"]; ?></a>
<?php } ?>
</div>
答案 2 :(得分:0)
SELECT DISTINCT column_name,column_name FROM table_name;
尝试此查询以获取记录