How to set id in jquery ui source code autocomplete event

I wanted to use an auto widget so that someone could select an employee by entering the employee name in the text box, but I want the form to publish the employee ID, not the employee name. data, that is, the names of employees as a source. the label and value in .js is the same as the source provided by me, how can I get the employee name and identifier separately.

  $("#txtEmployeeName").autocomplete({  
                   var suggestions =  [] ; 
                   //define callback to format results  
                   source: function(req, add){  
                   ajax call...                 
                   on success: function( data ) 
                   {                   
                      suggestions = data.split('|');
                      add(suggestions);
                   } 
                   select: function(e, ui) { 
                     //create formatted friend  
                     var friend = ui.item.value,
                         span = $("<span>").text(friend)  
                         $("#"+ $(this).attr("id")).val(span);  
                    } 

                });   
+5
source share
1 answer

You need to pass the json object to the original property.

Then your ui.item is an object, with Id, value, everything you want.

$(function() {                  
    var students = [{id: 1322,label: "student 1"},{id: 2,label: "Student 2"}];
    $( "#search-student" ).autocomplete({
        source: students,
        minLength: 2,
        select: function( event, ui ) {
              window.location.href="/?studentId=" + ui.item.id;
        }                   
    });             
});

jQuery getJSON: http://api.jquery.com/jQuery.getJSON/, json ajax.

+8

All Articles