在 jquery 自动完成中获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5766726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:47:25  来源:igfitidea点击:

Get value in jquery autocomplete

jqueryjquery-autocomplete

提问by Elankeeran

Here is my code

这是我的代码

jquery code

查询代码

$("input#shopName").autocomplete({
    source: "getShop.php",
    minLength: 2
});

The JSON value return from PHP as below

PHP 返回的 JSON 值如下

if(isset($_GET["term"])){

$query=$_GET["term"];
    $result = $dataset->get_custom_record("SELECT * FROM mc_shop WHERE shop_title like  '%" . $query . "%'  ORDER BY id");
}

 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $row_array['id'] = $row['id'];
        $row_array['value'] =$row['shop_title'];

        array_push($return_arr,$row_array);
    }
echo json_encode($return_arr);

autocomplete is working fine but while selecting the value from autocomplete I need put the corresponding "id" value inside one hidden variable I don't know how to do>

自动完成工作正常,但在从自动完成中选择值时,我需要将相应的“id”值放入一个隐藏变量中,我不知道该怎么做>

回答by karim79

$("input#shopName").autocomplete({
    source: "getShop.php",
    minLength: 2,
    select: function(event, ui) { 
        $("#theHidden").val(ui.item.id) 
    }
});

See http://jqueryui.com/demos/autocomplete/#event-select

http://jqueryui.com/demos/autocomplete/#event-select

回答by Zeeshan Ali

This works for me fantastically:

这对我来说非常有效:

$(ui)[0].item.label
$(ui)[0].item.value