如何在jquery ui自动完成中使用json传递隐藏的id?

也许它是重复的,但我找不到解决方案所以我发布了这个问题。我使用jquery ui自动完成搜索框。 它工作正常,但问题是我想使用id.example搜索:当用户输入paris时,我尝试在mysql中发送city_id进行搜索。 所以问题是如何用json传递隐藏的id?
这里的代码:

<input type="text" name="grno" id="grno" class="input" title=" 

jquery code:

  $(function() { function split( val ) { return val.split( /,\s*/ ); } function extractLast( term ) { return split( term ).pop(); } $( "#grno" ) // don't navigate away from the field on tab when selecting an item .bind( "keydown", function( event ) { if ( event.keyCode === $.ui.keyCode.TAB && $( this ).data( "ui-autocomplete" ).menu.active ) { event.preventDefault(); } }) .autocomplete({ source: function( request, response ) { $.getJSON( "pages/search.php", { term: extractLast( request.term ) }, response ); }, search: function() { // custom minLength var term = extractLast( this.value ); if ( term.length < 1 ) { return false; } }, focus: function() { // prevent value inserted on focus return false; }, select: function( event, ui ) { var terms = split( this.value ); // remove the current input terms.pop(); // add the selected item terms.push( ui.item.value ); // add placeholder to get the comma-and-space at the end terms.push( "" ); this.value = terms.join( "," ); alert(data.id); return false; } }); });  

autocomplete.php :

  

所以如何在autocomplete.php传递student_id ,比如标签和值。{label =’xyz’value =’1′}

在autocomplete.php中替换此代码

 array_push($return_arr,array("value"=>$obj['student_id'],"label"=>$obj['first_name']."(".$obj['grno'].")")); 

并在您的脚本中更改此内容

  terms.push( ui.item.label ); $( "#stud_id" ).val( ui.item.value ); 

希望,这就是你找到的。