[Autocomplete] issue when changing a previously selected value

[Autocomplete] issue when changing a previously selected value

[Autocomplete] issue when changing a previously selected value

I'm using autocomplete and have it using Json formatted replies.

It's working very smooth and I have it in place in several places all over my projects. Just found what looks like a bug but could also be a configuration error from my side.
The process is that Autocomplete takes what's input into the field, searches using the value and when a value is found and selected it populates the searched field with, say a name, and a hidden field with an id.
But it should also accept an input if there is no search results. If that's the case obviously nothing is put in the hidden id field.

This all works for the first round of search and display. But, if the user first finds a velue and thus the hidden id field gets populated and then changes the field content to something that's not found in the search, the hidden field content remains the same. I want it to be emptied in this case.

Have not found a way to trigger that.

Here's some code.

First some functions called:

var autocompleteJSON = function(raw) {
var json = typeof(raw) === "array" ? raw : raw.resultSet;
var parsed = [];
for (var i=0; i < json.length; i++) {
var row = json[i];
parsed.push({
data: row
});
}
return parsed;
};


var formatItem = function(row, position, length) {

return row["name"];
}


Then the jquery code:

$(function(){
// bind an auto complete to the org field
$("#orgname").autocomplete("ajax.lasso", {
extraParams: {
"actionType": "autolookup",
"action": "find_orgtolinkreport"
},
width: "500px",
minChars: 3,
dataType: "json",
parse: autocompleteJSON,

formatItem: formatItem,

selectFirst: false
});

$("#orgname").result(function(event, data, formatted) {
if (data) {
t = data["type"];
$("#orgname").val(data["name"]);
$("#orgref").val(data["ref"]);
$("#type option[value=" + t +"]").attr("selected","selected");
}
else { // this never triggers and is thus useless
$("#orgref").val("");
}
});

$("#orgname").focus(function() {
$("#orgname").flushCache(); 
});

$("#orgname").change(function() { // This doesn't trigger once a value's been selected for the field and later is changed
if($("#orgref").val().length == 0) {
alert("' + $lang_messages -> infoaboutnonreggedorgMsg + '");
}
});
});


The Json that's returned if nothing is found looks like:

{"resultSet":[]}

Can this be fixed somehow?