【问题标题】:jQuery autocomplete, and returning key for entered valuejQuery自动完成,并返回输入值的键
【发布时间】:2009-12-21 19:14:48
【问题描述】:

我在这里使用 jQuery 自动完成功能: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
}

LookupAction 返回键|值列表。 如果我添加一些按钮,以获取自动完成器中选定值的键,我会有这样的东西:

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

        return false;
}

虽然我可以看到通过 findValue 函数中的警报函数输入文本框值的键,但问题是:有可能以某种方式从那里返回它们吗? (即 var retVal = oSuggest.findValue())

谢谢!

【问题讨论】:

    标签: jquery jquery-autocomplete


    【解决方案1】:

    你试过了吗?

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

    请注意,我在函数末尾使用的符号称为“三元”。 You can find more information about it here.

    编辑:试试这个

    把它放在页面上的某个地方

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

    然后把 findValue 改成这个

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

    现在您可以通过引用$('#id_of_hidden_text_field').val();来引用选择的ID

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-19
      • 2014-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多