简体   繁体   中英

How to set attribute title on autocomplete

How to add title attribute to autocomplete and show title while mouse over through the option.

  <input type="text" id="txtitem" name="txtitem" class="form-control items"   onKeydown="$(this).autocomplete({source: 'demo1.php',minLength:0 });" />

demo1.php:

<?php
//database configuration
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbName = 'sample';

//connect with the database
$db = new mysqli($dbHost,$dbUsername,$dbPassword,$dbName);

//get search term
$searchTerm = $_GET['term'];

//get matched data from skills table
$query = $db->query("SELECT * FROM item_list WHERE product_name  LIKE '%".$searchTerm."%' GROUP BY product_name ORDER BY product_name ASC");
while ($row = $query->fetch_assoc()) {
    $data[] = $row['product_name'];
}

//return json data
echo json_encode($data);
?>

Add this just after your autocomplete:

.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
    return $( "<li></li>" ).data("item.autocomplete", item)
        .append( "<a title=" + item.product_name + ">" + item.product_name + "</a>")
        .appendTo( ul );
 }

JsFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM