jQuery的自动完成GET id作为选择的标签 [英] jquery autocomplete get id as selected label

查看:150
本文介绍了jQuery的自动完成GET id作为选择的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JQuery的自动完成在PHP中获得数据库的数据。
我从数据库中获取正确的结果,因为我键入关键字。但是,我想这些数据的ID分开(因为我不希望在标签本身的ID一样)。我JQUERY code lokks是这样的:

  $(#referrer).autocomplete({
    来源:函数(请求,响应){
        $阿贾克斯({
            URL:/ AJAX / ir_populate_referrer
            数据类型:JSON
            键入:POST,
            数据:{
                关键字:request.term
            },
            成功:功能(数据){
                响应($ .MAP(数据,功能(项目){
                    //alert(item.label);
                    返回{
                        标签:item.label
                    }
                }));
             }
        })
    }
});

PHP后端:

  $ searchArray =阵列();
    而($搜索= $ result->取()){
        $链接='';
        $链接。= $搜索['身份证']','。$搜索['cus_firstname'。 '。$搜索['cus_lastname']','$搜索。['cus_email'],'$搜索。['cus_phone1']';        array_push($ searchArray,阵列('标签'=> $连接,'值'=> $关键字,'ID'= GT; $搜索['身份证']));
    }回声json_en code($ searchArray);

问题是我怎样才能把ID比标签本身,当用户选择特定的建议其他HTML。我希望把id在HTML容器:

 <输入类型=隐藏的名字='referrer_id'ID ='referrer_id/>


解决方案

  $(#referrer).autocomplete({
    来源:函数(请求,响应){
        $阿贾克斯({
            URL:/ AJAX / ir_populate_referrer
            数据类型:JSON
            键入:POST,
            数据:{
                关键字:request.term
            },
            成功:功能(数据){
                响应($ .MAP(数据,功能(项目){
                    //alert(item.label);
                    返回{
                        标签:item.label,
                        值:item.value //编辑
                    }
                }));
             }
        })
    }
    选择:函数(事件,UI){
        $(#referrer_id)VAL(ui.item.value)。 // ui.item.value包含所选标签的id
    }
});

I am using JQuery autocomplete to get data from database in php. I am getting correct result from database as I type keyword. But, I want id of that data separate (because I don't want id in the label itself). My JQUERY CODE lokks like this:

$( "#referrer" ).autocomplete({
    source: function(request, response) {
        $.ajax({
            url: "/ajax/ir_populate_referrer",
            dataType: "json",
            type: "POST",
            data: {
                keyword: request.term
            },
            success: function(data){
                response( $.map( data, function( item ) {
                    //alert(item.label);
                    return {
                        label: item.label
                    }
                }));
             }
        })
    }
});

PHP Backend:

$searchArray = array();
    while($search = $result->fetch()){
        $link = '';
        $link .= $search['id'].', '.$search['cus_firstname'].' '.$search['cus_lastname'].', '.$search['cus_email'].', '.$search['cus_phone1'];

        array_push($searchArray, array('label'=> $link, 'value' => $keyword, 'id'=>$search['id']));
    }

echo json_encode($searchArray);

The problem is how can I put id in html other than label itself, when user selects particular suggestion. I want to put id in this HTML container:

<input type='hidden' name='referrer_id' id='referrer_id' />

解决方案

$( "#referrer" ).autocomplete({
    source: function(request, response) {
        $.ajax({
            url: "/ajax/ir_populate_referrer",
            dataType: "json",
            type: "POST",
            data: {
                keyword: request.term
            },
            success: function(data){
                response( $.map( data, function( item ) {
                    //alert(item.label);
                    return {
                        label: item.label,
                        value: item.value     // EDIT
                    }
                }));
             }
        })
    }
    select: function(event, ui) {
        $("#referrer_id").val(ui.item.value);  // ui.item.value contains the id of the selected label
    }
});

这篇关于jQuery的自动完成GET id作为选择的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆