Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在每个循环中的jQuery中使用autocomplete?_C#_Asp.net Mvc_Jquery Ui - Fatal编程技术网

C# 如何在每个循环中的jQuery中使用autocomplete?

C# 如何在每个循环中的jQuery中使用autocomplete?,c#,asp.net-mvc,jquery-ui,C#,Asp.net Mvc,Jquery Ui,我正在使用jQuery生成一个表。在每一行中我都有一个输入字段,它应该从DB usibg autocomplete中搜索一些值。第一行是perfectley,但第二行有问题,它在autocomplete中只返回上面字段中的一个值,我不知道如何修复它。。。有人能帮忙吗 这是我的密码: $.ajax({ type: "GET", url: "/Home/JsonWeekEvents", dataType: "JSON", success: function (res

我正在使用jQuery生成一个表。在每一行中我都有一个输入字段,它应该从DB usibg autocomplete中搜索一些值。第一行是perfectley,但第二行有问题,它在autocomplete中只返回上面字段中的一个值,我不知道如何修复它。。。有人能帮忙吗

这是我的密码:

 $.ajax({
    type: "GET",
    url: "/Home/JsonWeekEvents",
    dataType: "JSON",
    success: function (result) {
        $.each(result, function (i, val) {

            var trow = $('<tr/>').data("id", val.Id);
            //trow.append('<td>' + val.Id + "&nbsp;" + '</td>');
            trow.append('<td style="padding:5px; width:100px; height:70px"></td>');
            trow.append('<td valign="top" style="padding:2px; width:150px; height:100px">' +
                            '<div class="ui-widget">' +
                                    '<input  size="10" maxlength="10" id="tagsM" class="tags" />' +
                                        '<input type="button" id="addBtn" onclick="addEvent();" size="5" value="+" /><br/>' +
                                             '<div style="text-align:center" id="desc_Num">' + val.Monday + '</div >' +
                            '</div >' +
                        '</td>');

            tab.append(trow);
        });

        $("tr:odd", tab).css('background-color', '#C4C4C4');
        $("#weekEvents").html(tab);
    },
    error: function () {
        alert("Failed! Please try again.");
    }
});
var tab = $('<table class=MyTable border=1 ></table>');
var thead = $('<thead></thead>');

thead.append('<th style="padding:5px">FSE' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Monday' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Tuesday' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Wednesday' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Thursday' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Friday' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Saturday' + "&nbsp;" + '</th>');
thead.append('<th style="padding:5px">Sunday' + "&nbsp;" + '</th>');

tab.append(thead);


tab.on("focus", "input[class='tags']", function (e) {

    //var prefix = $('.tags').val();

    $('.tags').autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Home/GetSearchValue",
                dataType: "json",
                data: { search: $('.tags').val() },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {

                            label: item.Title + ', ' + item.Description, value: item.Title,

                            Id: item.Id,
                            Title: item.Title,
                            Description: item.Description,
                            Location: item.Location
                        }
                    }));
                },
                error: function (xhr, status, error) {
                    alert("Error!" + xhr);
                },

            });
        }
    });

建议如下:

tab.on("focus", "input[class='tags']", function(e) {
  if (!$(this).hasClass("ui-autocomplete")) {
    $(this).autocomplete({
      source: function(request, response) {
        $.getJSON("/Home/GetSearchValue", {
            search: request.term
          },
          function(data) {
            response($.map(data, function(item) {
              return {
                label: item.Title + ', ' + item.Description,
                value: item.Title,
                Id: item.Id,
                Title: item.Title,
                Description: item.Description,
                Location: item.Location
              }
            }));
          });
      }
    });
  }
});

这将初始化焦点事件上的自动完成。如果已经初始化,则不会重复。源将执行GET请求并从用户在该特定字段上的输入中搜索request.term。

欢迎使用堆栈溢出。请提供一个最小的、可重复的示例:我还建议您进行参观:我怀疑您的问题源于以下数据:{search:$'.tags'.val}。如果有超过1个元素带有tags类,这可能会导致意外的结果。我也这么认为,但如何避免它并生成唯一的类ir id名称呢?你知道吗?你可以用request.term来代替。请看:我会试试这个,谢谢advice@Dimitri我很高兴这有帮助。我希望你能对答案进行投票并打分。我是新来的。我已经做了标记,但不知道如何才能提高投票率…@Dimitri在答案的左边,有上下箭头来提高投票率或降低投票率。这一切都在旅行中:哦,好吧,我认为它不是真正可见的:谢谢你的反馈!声誉低于15的人所投的票将被记录,但不会改变公开显示的帖子分数。