也许它是重复的,但我找不到解决方案,所以我发布了这个问题。我使用jQuery UI自动完成搜索框。它工作正常,但问题是我想使用id.example进行搜索:当用户键入巴黎,我尝试在mysql中发送city_id进行搜索。所以问题是如何通过隐藏ID与JSON?
这里的代码:如何在jQuery UI自动完成中使用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.how传递student_id
在autocomplete.php
,如标签和值{标签= 'XYZ' 的值。 ='1'}
您不包括隐藏的ID或city_id的html代码。然而,一个想法是将隐藏的id值添加到json-url,例如“pages/search.php?hidden_id =”+(“#hidden_id”)。val(),然后您可以通过search.php $ _GET ['hidden_id'] – 2013-03-04 12:06:06
不,我的问题是,在search.php中,我得到了first_name和grno,它显示在自动完成列表中,但如何使用'student_id'进行搜索?像选择框,这样的contry名称是显示但是按contry_id搜索。 – DS9 2013-03-04 12:11:35
你的问题不是很清楚!但看看你的mysql代码,它不搜索使用student_id:...其中'grno'像'%'。$ term。'%'或'first_name'就像'%'。$ term。“%'--- >您可以在最后添加学生ID:或student_id = $ some_id – 2013-03-04 12:32:23