I have a JQuery suggest box using a key and a value. The key is the saved value, for example a userId and the value is the shown value such as a username.
When having a blank field it works great. I type a few characters, select a name and the value is added as the value that is posted with the HTTP request. Now, how should I prefill a form’s suggest with when it already has a value. When placing the saved userId as the value the suggest shows the userId but, obviously I want to show the username. I also tried to echo the username that was selected but than, if the username is not changed the posted value will be the username.
<script>
<!--
$(document).ready(function() {
$("#creatorUserId").autocomplete("Gateway.php?action=UserAction&subAction=suggest",{
parse: function(data) {
var parsed = [];
data = data.data;
for (var i = 0; i < data.length; i++) {
parsed[parsed.length] = {
data: data[i],
value: data[i].key,
result: data[i].value
};
}
return parsed;
},
formatItem:function(item, index, total, query){
return item.value;
},
formatResult:function(item){
return item.id;
},
dataType: 'json'
});
});
-->
</script>
<input type="text" name="creatorUserId" id="creatorUserId" value="3" size="40" />
How could I solve this?
Try this