我有一个 JSON 字符串
{"Name1":"ID1","Name2":"ID2"}
,就像我使用 PHP 的 json_encode 检索到的一样。如何在自动完成选项中有一个带有 Name1,Name2 的输入字段,并且一旦选择Name1,ID1将被置于隐藏字段中?我正在使用 Jquery ui 自动完成功能。
var NameIDJsonString = <?php echo $NameIDJsonString; ?>;
$(function () {
$('#JSONName').autocomplete({
source: function (request, response) {
response($.map(NameIDJsonString, function (value, key) {
return {
label: key,
value: value
};
}));
},
select: function (event, ui) {
$("#JSONName").val(ui.item.text); // display the selected text
$("#JSONID").val(ui.item.value); // save selected id to hidden input
}
});
});
}
<html>
<body>
<input id="JSONName" name="JSONName" size="30" class="ui-autocomplete-input" autocomplete="on" type="text" >
<input id="JSONID" name="JSONID" size="30" class="ui-autocomplete-input" autocomplete="on" type="hidden" >