【问题标题】:How to get 2 value from database using jquery autocomplete如何使用 jquery 自动完成从数据库中获取 2 个值
【发布时间】:2017-06-03 12:58:44
【问题描述】:

如果我不能说清楚,请提前抱歉。

我有一个包含 2 列 item_descriptionitem_price 的表格,我有 2 个 input 标签,一个用于 item name,另一个用于 price

如果用户键入 item name 并从 autosuggest price 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',
});

希望能得到一些帮助,谢谢。

【问题讨论】:

  • @gerry thnx 兄弟编辑我的帖子
  • 称某人为 bro 听起来不太专业,对方可能会因此受到轻微冒犯。只是下次交流的提示。 :)

标签: php jquery mysqli autocomplete


【解决方案1】:

希望我明白你的意思, 您可以随时编写自己的自动完成功能

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-24
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 2015-11-26
    相关资源
    最近更新 更多