views:

339

answers:

1

Hello. I am using jQuery autocomplete from here : http://www.pengoworks.com/workshop/jquery/autocomplete.htm

        $("#TestTextbox").autocomplete(
             '<%= Url.Action("LookupAction") %>',
             {
              delay:10,
              minChars:1,
              matchSubset:1,
              cacheLength:0,
              onItemSelect:selectItem,
              onFindValue:findValue,
              formatItem:formatItem,
              autoFill:false
             }
            );

function findValue(li) 
{
        if( li == null )     
            return alert("No match!");

        if( !!li.extra ) 
            var sValue = li.extra[0];
        else 
            var sValue = li.selectValue;

       alert(sValue);
}

function selectItem(li) 
{
        findValue(li);
}
function formatItem(row) 
{
        return row[0]; //value
}

the LookupAction return key|value list. if I add some button, to get the key for selected value in autocompleter, i'll have something like this :

function lookupAjax()
{
        var oSuggest = $("#TestTextbox")[0].autocompleter;
        oSuggest.findValue();

        return false;
}

while i can see key for entered to textbox values via alert functions in findValue function, the question is : it possible to return them from there somehow ? (i.e. var retVal = oSuggest.findValue())

Thank You !

+1  A: 

Have you tried this?

function findValue(li) 
{
        if( li === null ){           
           alert('No match found!');
           return false;
        }
        return ( !!li.extra ) ? li.extra[0] : li.selectValue; 
}

Please note that the notation I have used at the end of the function is called "ternary". You can find more information about it here.

EDIT: Try this

Put this on the page somewhere

<input type="hidden" id="id_of_hidden_text_field" />

Then change findValue to this

function findValue(li) 
{
        if( li === null ){           
           alert('No match found!');
        }
        $('#id_of_hidden_text_field').val(( !!li.extra ) ? li.extra[0] : li.selectValue); 
}

Now you can refer to the chosen ID by referring to $('#id_of_hidden_text_field').val();

Gausie
Hi. Thanks for reply. Yep, tried this with no luck =[.in documentation i have read that the "findValue function can potentially perform an AJAX operation, therefore the findValue() function does not return a value. Instead, you need to specific a onFindValue callback function that will run."
vbobruisk
Ah ok, see my edit
Gausie