在jquery自动完成中获取值

时间:2011-04-23 20:26:55

标签: jquery jquery-autocomplete

这是我的代码

jquery代码

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

从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);

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

3 个答案:

答案 0 :(得分:13)

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

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

答案 1 :(得分:0)

使用以下内容获取onClick的选定值

   $( "#searchText" ).bind( "autocompleteselect", function(event, ui) {
    console.log(ui['item']['value']);
    }); 

答案 2 :(得分:-2)

这对我很有用:

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