如何在jQuery UI自动完成中使用json传递隐藏的ID? [英] how to pass hidden id using json in jquery ui autocomplete?

查看:116
本文介绍了如何在jQuery UI自动完成中使用json传递隐藏的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

perhaps it is duplicate,but i can't found solution so i posted this question.I am use jquery ui for auto complete search box. it works fine but the problem is i want to search using id.example:when user type paris,i try to send city_id in mysql for search. so problem is how to pass hidden id with json?
here the code:

 <input type="text" name="grno" id="grno" class="input" title="<?php echo $lng['vldgrno'];?>

jquery code:

<script>
 $(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;
    }
  });
  });
  </script>

autocomplete.php :

<?php
  mysql_connect('localhost','root','');
 mysql_select_db('school');
 $return_arr = array();
 $term = trim(strip_tags($_GET['term']));//retrieve the search term that autocomplete sends
 //create select query
  $query = "select `grno`,`first_name`,`student_id` from `tbl_student` where `grno` like '%".$term."%' or `first_name` like '%".$term."%'";
  // Run the query
  $result = mysql_query ($query);
  $array = array();
 while($obj = mysql_fetch_array($result)){
 $array['name'] = $obj['first_name']."(".$obj['grno'].")";
 array_push($return_arr,$obj['first_name']."(".$obj['grno'].")");
 }  
 $json = json_encode($return_arr);
 echo $json;
 ?>

因此,如何在autocomplete.php中传递student_id,如标签和值.{label ='xyz'value ='1'}

so.how to pass student_id in autocomplete.php,like label and value.{label='xyz' value='1'}

推荐答案

在autocomplete.php中替换此代码

In autocomplete.php replace this code

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

并在您的脚本中对此进行更改

and in your script change this

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

希望,这就是您所发现的.

hope,this is what you find.

这篇关于如何在jQuery UI自动完成中使用json传递隐藏的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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