代码之家  ›  专栏  ›  技术社区  ›  iDipa

JQuery:autocomplete未填充类型上的数据

  •  0
  • iDipa  · 技术社区  · 5 年前

    我在razor HTML页面的viewbag中有一些数据。由此,我准备了key=>value thing的数组对象。但当我尝试将其传递给autocomplete源代码时,它不会给出任何错误,也不会填充任何下拉列表中的数据。代码如下。。。

    myAssociativeArr : 
    0: {key: "4852, 30", value: "PortBlair, Andaman and Nicobar Islands"}
    1: {key: "4992, 1", value: "Anantapur, Andhra Pradesh"}
    2: {key: "5093, 1", value: "Eluru, Andhra Pradesh"}
    3: {key: "4975, 1", value: "Guntur, Andhra Pradesh"}
    4: {key: "4982, 1", value: "Kadapa, Andhra Pradesh"}
    5: {key: "4825, 1", value: "Kakinada, Andhra Pradesh"}
    6: {key: "5090, 1", value: "Nandyal, Andhra Pradesh"}
    7: {key: "4988, 1", value: "Nellore, Andhra Pradesh"}
    8: {key: "4846, 1", value: "Ongole, Andhra Pradesh"}
    9: {key: "5091, 1", value: "Proddatur, Andhra Pradesh"}
    10: {key: "4963, 1", value: "Rajahmundry, Andhra Pradesh"}
    
    <div id="FullFilterCorner" class="col-sm-12">
    <label class=""> Filter</label> <br />
    <input id="FullFilter1" />
    </div>
    
    <script>
            $(function () {
    var myAssociativeArr = [];
                for (var i = 0; i < States.length; i++) {
                    var stateLocations = locations.filter(x => x.StateId == States[i].Id);
                    for (var j = 0; j < stateLocations.length; j++) {
    
                        var newElement = {};
                        newElement['key'] = stateLocations[j].BranchId + ', ' + stateLocations[j].StateId;
                        newElement['value'] = stateLocations[j].BranchName + ', ' + States[i].State;
    
                        myAssociativeArr.push(newElement);
                    }
                }
    
    $("#FullFilter1").autocomplete({
                    minLength: 0,
                    source: myAssociativeArr,
                    focus: function (event, ui) {
                        $("#FullFilter1").val(ui.item.value);
                        return false;
                    },
                    select: function (event, ui) {                    
                        $("#FullFilter1").val(ui.item.value);
                        return false;
                    }
                }).data("ui-autocomplete")._renderItem = function (ul, item) {
                    return $("<li>")
                        .append("<a>" + item.value + "<br>" + item.key + "</a>")
                        .appendTo(ul);
                };
                }
            </script>
    

    无法解决。

    0 回复  |  直到 5 年前