2017-06-03 38 views
0

對不起,如果我無法說清楚。如何使用jquery自動完成功能從數據庫中獲取2值

我有2列item_descriptionitem_price一個表,我有2個input標籤,一個是項目名稱等爲價格

我使用jQuery自動完成讓item_description現在我想要什麼,如果用戶類型項目名稱並選擇一個從自動提示價格input字段應自動填充選定項目名稱

這裏是我的PHP頁面:

$searchTerm = $_GET['term']; 
$query = $db->query("SELECT * FROM items WHERE item_description LIKE '%".$searchTerm."%' ORDER BY item_description ASC"); 

while ($row = $query->fetch_assoc()) { 
    $data[] = $row['item_description']; 
    $data[] = $row['item_price']; 
} 

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

這裏是JS:

$(".itemName").autocomplete({ 
    source: '../ajxphp/itemsname_autoSuggest.php', 
}); 

希望能得到一些幫助,感謝名單提前。

+0

@gerry thnx bro編輯我的帖子 –

+0

打電話給某個* bro *聽起來不太專業,另一個人可能會略微冒犯它。只是下次通信提示。 :) –

回答

0

希望我理解你的意思, 你總是可以編寫自己的自動完成功能

JS:

$(document).ready(function(){ 

    var jsonAccArr = {}; 
    function Init() { 
     var json = [ 
         { 'item_description': 'shlomi', 'item_price': 3 }, 
         { 'item_description': 'yoni', 'item_price': 8 }, 
         { 'item_description': 'david', 'item_price': 1 }, 
         { 'item_description': 'roni', 'item_price': 5 }, 
        ]; 

     json.forEach(function(element) { 
      jsonAccArr[element['item_description']] = element['item_price']; 
      $("#items").append("<option value='" + element['item_price'] + "'>" + element['item_description'] + "</option>"); 
     }); 
     var selectedName = $("#items option:selected").text(); 
     $("#price").html(jsonAccArr[selectedName]); 
    } 


    Init(); 
    $("#items").change(function() { 
     var selectedName = $("#items option:selected").text(); 
     $("#price").html(jsonAccArr[selectedName]); 
    }); 

}); 

HTML:

<div class="ui-widget"> 
    <label for="items">Product: </label> 
    <select id="items"> 
    </select> 

    <br> 

    <label for="price">Price:</label> 
    <span id="price"></span> 
</div> 

的jsfiddle鏈接: Code Example

相關問題