2017-06-03 70 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

相关问题