下面是我的自动完成文本框代码。
代码工作正常..。
,但我也需要将选定值的id存储在隐藏的文本框..中
代表EX =
我的自动文本框中有两个值
id societyname
7 raj
15 lucky如果我从上面的值中选择raj,那么在隐藏的文本框中显示raj :7的id。
请大家帮帮我。
自动完成文本框
<input id="society_name" name="society"/>
<input type="hidden" id="society_name" name="societyid"/>In ajax.php
if($_GET['type'] == 'society'){
$result = mysql_query("SELECT society FROM societymaster where society LIKE '".strtoupper($_GET['name_startsWith'])."%'");
$data = array();
while ($row = mysql_fetch_array($result)) {
array_push($data, $row['society']);
}
echo json_encode($data);
}auto.js
$('#society_name').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'society'
},
success: function( data ) {
response( $.map( data, function( item ) {
return {
label: item,
value: item
}
}));
}
});
},
autoFocus: true,
minLength: 0
});发布于 2015-04-03 11:51:28
在输入上使用val()函数,从ajax结果传递id
$('#society_name').val(data.id);更新代码:
php:
if($_GET['type'] == 'society'){
$result = mysql_query("SELECT id,society FROM societymaster where society LIKE '".strtoupper($_GET['name_startsWith'])."%'");
$data = array();
while ($row = mysql_fetch_array($result)) {
array_push($data,$row);
}
echo json_encode($data);
}联署材料:
$('#society_name').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'society'
},
success: function( data ) {
$('#society_name').val(data[0].id);
response( $.map( data, function( item ) {
return {
label: item,
value: item
}
}));
}
});
},
autoFocus: true,
minLength: 0
});发布于 2015-04-03 12:19:53
如果您需要ID,那么从DB中获取它们并在ajax调用中返回(我想这就是您正在讨论的ID)。从这里开始,您可以使用类似这样的方法来缓存返回的结果,例如关联数组:$arrvalue = id;如果用户选择其中一个值,您可以获得相应的ID。
顺便说一下。对未来的一些建议: 1. id的字段应该具有唯一的值。2.您应该考虑保护查询不受sql攻击。在这里,您甚至不转义由用户提供的字符串。3.据我所知,mysql已经过时,所以尝试使用mysqli。
ajax.php
if($_GET['type'] == 'society'){
// try to switch to mysqli. You can also consider prepare statements.
$result = mysql_query("SELECT id, society FROM societymaster where society LIKE '".mysql_real_escape_string(strtoupper($_GET['name_startsWith']))."%'");
$data = array();
while ($row = mysql_fetch_array($result)) {
array_push($data, $row);
}
// mysql_fetch_array returns indexed array so we should get JSON string like: "[['id1', 'value1'], ['id2', 'value2'], ...]"
echo json_encode($data);
}.js文件
$.arr = [];
$('#society_name').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'ajax.php',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'society'
},
success: function( data ) {
// clear cache array and fill it with information about the new results (societies and corresponding ids)
$.arr = [];
$.each(data, function (idx, item) {
$.arr[item[1]] = item[0];
});
$('#society_name').val(data[0].id);
response( $.map( data, function( item ) {
return {
label: item,
value: item
}
}));
}
});
},
autoFocus: true,
minLength: 0
});
// catch an event when the autocomplete closes and get id for selected society name
$("#tags").on("autocompleteclose", function() {
// close event does not provide object data so we must fetch it again
var selectedValue = $("#tags").val();
var id = $.arr[selectedValue];
// now you can set this ID as the value of your hidden input
});https://stackoverflow.com/questions/29431608
复制相似问题