开发者

jQuery autocomplete, and returning key for entered value

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 = $("#TestT开发者_运维知识库extbox")[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 !


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();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜