對於搜索選項,您可以執行以下操作:
HTML:
<div class="pull-right" style='display:block;'>
<span style="color:red"><strong>Search Products</strong></span>
<form method="get">
<input name="keysearch" value="" placeholder="name" id="keysearch" type="text" class="form-control">
<span id="loading"></span>
</form>
<div id="result"></div>
</div>
腳本中使用:
<script>
$(document).ready(function(){
var req = null;
$('#keysearch').on('keyup', function(){
var key = $('#keysearch').val();
if (key && key.length > 0){
$('#loading').css('display', 'block');
if (req)
req.abort();
req = $.ajax({
url : 'fetch_records.php',
type : 'GET',
cache : false,
data : {
keysearch : key,
},
success : function(data)
{
console.log(data)
if (data)
{
$('#loading').css('display', 'none');
$("#result").html(data).show();
$("#result").css('position', 'absolute');
$("#result").css('z-index', '1');
// style='display:block; position :absolute; z-index:1'
}
}
});
}
else
{
$('#loading').css('display', 'none');
$('#result').css('display', 'none');
}
});
});
</script>
PHP取回records.php文件:
<?php
$conn = mysqli_connect('localhost','root', '','Name_OF_DB');
if(isset($_GET['keysearch']))
{
$search = $_GET['keysearch'];
$search = mysqli_real_escape_string($conn,$search);
// $data = "SELECT * FROM products ";
$data = "SELECT * FROM products WHERE product_title LIKE '%{$search}%' order by product_id ";
// $query = query ("SELECT * FROM products WHERE product_category_id = ".$id." ORDER BY product_price DESC");
$result = mysqli_query($conn,$data);
if (mysqli_num_rows($result)>0)
{
while($row= mysqli_fetch_assoc($result))
{
echo"<a href='create_a_new_page_brother.php?id={$row['product_id']}&price=0' class='list-group-item'>{$row['product_title']}</a>";
}
}
}
?>
謝謝!有沒有一種方法可以獲得按下的按鍵,而不是盒子的全部價值? – balloway
沒關係,只要閱讀你附加的文檔,它就能解決所有問題。再次感謝! – balloway
即時編輯與按鍵事件代碼 –