views:

46

answers:

1

So I have a jquery autocomplete on a testbox that searches the database for a user, and places the ID in a hidden input field. For some reason the autocomplete fires when they type something, then again when an item is selected, changing the text. Since I'm searching first and last name it doesn't find any matches the second time and clears my field out.

I have it working on a separate page, but that only searches one field, so when it returns back a value it stays. This however searches two fields, so when it goes to search a second time it doesn't return anything. I believe the problem lies there, but I don't know how to fix it.

    $("#FormSubscriberName").autocomplete('/subscriber/search', {
    autoFill: false,
    mustMatch: true,
    matchContains: true,
    cacheLength: 12,
    formatItem: function (data, index, max) {
        return data[1];
    },
    formatMatch: function (data, index, max) {
        return data[1];
    },
    formatResult: function (data, index, max) {
        return data[1];
    }
}).result(function (event, data, formatted) {
    if (data) {
        $("#SubscriberID").val(data[0]);
        $("#FormSubscriberName").val(data[1]);
    }
    else {
        $("#SubscriberID").val('<%= Guid.Empty %>');
    }
});


<input id="FormSubscriberName" name="FormSubscriberName" style="width:250px;" type="text" value="" /> 
<input id="SubscriberID" name="SubscriberID" type="hidden" value="" />
+1  A: 

I also encountered some odd behavior with autocomplete recently. My issue was resolved when I removed the "mustMatch" which is gone from jQuery UI autocomplete. Removing that option may be worth a try.

lcrepas
It did relate to this, it was firing again when I selected something. It would fire because the value had changed, even though it changed because I selected something.
Jhorra