Get value in jquery autocomplete

人走茶凉 提交于 2019-12-17 19:39:29

问题


Here is my code

jquery code

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

The JSON value return from PHP as below

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>


回答1:


$("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




回答2:


This works for me fantastically:

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


来源:https://stackoverflow.com/questions/5766726/get-value-in-jquery-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!