开发者

How to employ jquery autocomplete with ID/value pairs using a hidden field

There are several questions regarding my problem, but somehow none could actually help me solve it. I'm not that familiar with javascript let alone jquery so please bear with me.

As stated I have a text field with jquery autocomplete in place, which works just great. But I need to 开发者_开发知识库have a hidden field populated with the corresponding ID to the selected value. Here's the form:

<input name="clubname" id="clubname" type="text" />
<input name="clubid" id="clubid" type="hidden" value="" />

For the autocomplete I use an array like this: {id: 1, value: 'something' }. The whole page is in php getting the data from the DB and writing the array. Into the autocomplete I added the accented text feature. Here's the code:

var accentMap = {
    'á': 'a',
    'ä': 'a',
    'à': 'a',
    'â': 'a',
    'ö': 'o',
    'ô': 'o',
    'ó': 'o',
    'ò': 'o',
    'ü': 'u'
};

var normalize = function (term) {
    var ret = '';
    for (var i = 0; i < term.length; i++) {
        ret += accentMap[term.charAt(i)] || term.charAt(i);
    }
    return ret;
};

$().ready(function () {
    $('#clubname').autocomplete({
        source: function (request, response) {
            var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), 'i');
            response($.grep(clubs, function (value) {
                value = value.label || value.value || value;
                return matcher.test(value) || matcher.test(normalize(value));
            }));
        }
    });
});
});

It seems I have to use the result handler to get the ID into the hidden field. But how to exactly access the ID of the selected value escapes me.

Any help would be greatly appreciated!


Hope this solution helps you

$JsonArray =array();
foreach($EmployeeDetailList as $value){
    $Array[label]=$value['user_name'];
    $Array[value]=$value['user_name'];
    $Array[eid]=$value['id']; //Here goes the ID of the employee
    array_push($JsonArray,$Array);
}
$JsonSerialize=json_encode($JsonArray);

This was the server side coding to populate the Json encoded array

Now the next portion implements the JQuery Part

$(function() {
var availableTags = <?php echo $JsonSerialize;?>;
$( "#imas" ).autocomplete({
source: availableTags,
select: function(event,ui){
    var userid = ui.item.eid;
    alert(userid); //This will alert the Employee ID . You can use it any where you want 
},
});
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜