Javascript 选择自动完成项目无效

Javascript 选择自动完成项目无效,javascript,jquery,json,razor,autocomplete,Javascript,Jquery,Json,Razor,Autocomplete,我的asp.net mvc web应用程序中有以下字段:- <input class="push-up-button searchmargin" placeholder="Search by tag.." name="searchTerm2" data-autocomplete-source= "@Url.Action("AutoComplete", "Home")" type="text" style="margin-top:8px"/> 例如,如果我开始键入以下单词“i am

我的asp.net mvc web应用程序中有以下字段:-

<input  class="push-up-button searchmargin" placeholder="Search by tag.." name="searchTerm2" data-autocomplete-source= "@Url.Action("AutoComplete", "Home")" type="text" style="margin-top:8px"/>
例如,如果我开始键入以下单词“i am”,然后从自动完成列表结果中选择“i am writing”,则自动完成字段将具有“i am”文本,而不是选择“i am writing”。有谁能告诉我,是什么导致了这个问题

谢谢

编辑

通过添加焦点和选择,我将自动完成编辑如下:-

 $("input[data-autocomplete-source]").each(function () {
        var target = $(this);
        target.autocomplete({
            source: target.attr("data-autocomplete-source"), minLength: 1, delay: 1000,
            focus: function (event, ui) {
                $("input[data-autocomplete-source]").val(ui.item.label);
                return false;
            },
            select: function (event, ui) {
                $("input[data-autocomplete-source]").val(ui.item.label);
                return false;
            },

            create: function () {
                $(this).data("autocomplete")._renderItem = function (ul, item) {
                    return $('<li>').append('<a>' + '<b>'+item.label + '</b><br>' + '<span style="color:#8e8e8e ">' + item.resourcename + ' | ' + item.customername + ' | ' + item.sitename + '<hr style="padding: 0px; margin: 0px;">' + '</span></a>')
                                    .appendTo(ul);
                };
            }

        });
    });
$(“输入[数据自动完成源]”)。每个(函数(){
var目标=$(此);
target.autocomplete({
源:target.attr(“数据自动完成源”),最小长度:1,延迟:1000,
焦点:功能(事件、用户界面){
$(“输入[数据自动完成源]”).val(ui.item.label);
返回false;
},
选择:功能(事件、用户界面){
$(“输入[数据自动完成源]”).val(ui.item.label);
返回false;
},
创建:函数(){
$(此).data(“自动完成”)。\u renderItem=函数(ul,项){
返回$(“
  • ”).append(“+”+item.label+”
    “+”+item.resourcename+“|”+item.customername+“|”+item.sitename+”
    ”+) .附录(ul); }; } }); });
  • 但当我尝试选择自动完成项目时,会出现以下错误:-

    TypeError: item is undefined
    [Break On This Error]   
    
    self.element.val( item.value );
    
    TypeError:ui.item未定义

    请添加“选择”事件并在下面尝试

     $( "input[data-autocomplete-source]" ).autocomplete({
           select: function( event, ui ) {
             $( "input[data-autocomplete-source]" ).val( ui.item.yourValueProperties);
             return false;
          }
      });
    
    ***Note : yourValueProperties= like customername *** 
    

    这将在firebug上引发以下异常:TypeError:ui.item未定义[在此错误上中断]$(“输入[数据自动完成源])).val(ui.item.label);您是否获得ui.item==null??正如我使用r代码提到的,我将获得TypeError:ui.item未定义[在此错误上中断]$(“输入[数据自动完成源]”).val(ui.item.label);好的,John,我可以看到您的测试代码,因为数据将是动态的(MVC)。请检查此链接可能会获得解决方案请添加事件焦点:函数(事件,ui){$(“输入[数据自动完成源]”).val(ui.item.label);返回false;},然后重试
     $( "input[data-autocomplete-source]" ).autocomplete({
           select: function( event, ui ) {
             $( "input[data-autocomplete-source]" ).val( ui.item.yourValueProperties);
             return false;
          }
      });
    
    ***Note : yourValueProperties= like customername ***