【问题标题】:how to pass hidden id using json in jquery ui autocomplete?如何在 jquery ui 自动完成中使用 json 传递隐藏的 id?
【发布时间】:2013-03-04 11:53:55
【问题描述】:

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

 <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;
 ?>

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

【问题讨论】:

  • 你没有包括你的隐藏 id 或 city_id 的 html 代码。然而,一个想法是将隐藏的 id 值添加到 json-url,例如“pages/search.php?hidden_​​id=" + ("#hidden_​​id").val(),然后你可以从 search.php 访问它$_GET['hidden_​​id']
  • 不,我的问题是,在 search.php 中,我得到了 first_name 和 grno,这显示在自动完成列表中,但是如何使用 student_id 搜索?就像选择框一样,显示这样的国家名称但按 contry_id 搜索。
  • 你的问题不是很清楚!但看看你的 mysql 代码,它不使用 student_id 搜索: ...where grno like '%".$term."%' or first_name like '%".$term."%' --- > 您可以在最后添加学生 ID:或 student_id = $some_id

标签: php jquery json jquery-ui jquery-autocomplete


【解决方案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 );

希望,这就是你找到的。

【讨论】:

  • 但是当我从自动建议中选择任何选项时,它是项目的显示 ID。如何显示项目的标签,但当我按搜索时它应该传递项目的 ID?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2011-08-18
  • 2018-01-18
  • 2012-07-11
  • 2015-12-25
  • 1970-01-01
  • 2021-09-27
相关资源
最近更新 更多