0

您好,我已经根据搜索条件填充了文本框,即如果我键入 a,那么以 a 开头的项目将填充在文本框中,并且我使用了另一个文本框,其中我显示了派对名称,但问题是当我在项目文本框中选择值时它不在文本框中显示,但派对名称文本框正在工作,即当我从下拉列表中选择派对名称时,它显示在隐藏字段和文本框中,但如果有项目,它不起作用,我正在发送我的 jquery 函数,所以请帮忙

$(document).ready(function() {
    SearchText();
    SearchItem();
});

function SearchText() {
    $('input[name$="tbAuto"]').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "PartyList.asmx/FetchPartyList",
                data: "{ 'prefix': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    response(data.d);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 1,
        focus: function(event, ui) {
            $('input[name$="tbAuto"]').val(ui.item.Name);
            return false;
        },
        select: function(event, ui) {
            $('input[name$="tbAuto"]').val(ui.item.Name);
            $('input[name$="tbHidden"]').val(ui.item.value);
            return false;
        }
    }).data('autocomplete')._renderItem = function(ul, item) {
        return $('<li>').data('item.autocomplete', item).append('<a>' + item.Name + '</a>').appendTo(ul);
    };
}


function SearchItem() {
    $('input[name$="txtitem"]').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "Itemslist.asmx/FetchItemList",
                data: "{ 'prefix': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    response(data.d);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 1,
        focus: function(event, ui) {
            $('input[name$="txtitem"]').val(ui.item1.Name);
            return false;
        },
        select: function(event, ui) {
            $('input[name$="txtitem"]').val(ui.item1.Name);
            $('input[name$="hditem"]').val(ui.item1.value);
            return false;
        }
    }).data('autocomplete')._renderItem = function(ul, item1) {
        return $('<li>').data('item1.autocomplete', item1).append('<a>' + item1.Name + '</a>').appendTo(ul);
    };
}

项目信息类:

public class Iteminfo
{
    connection oConnection = new connection();
    Control oControl = new Control();
    AccountInfo oAccount = new AccountInfo();
    connection c = new connection();

    public string Title { get; set; }
    public string Name { get; set; }
    public string value { get; set; }

    public List<Iteminfo> GetItems(string prefixText)
    {
        List<Iteminfo> itemList = new List<Iteminfo>();

        try
        {
            DataTable dt;
            AccountInfo oAccount = new AccountInfo();
            //dt = oAccount.GetAccountInfo((int)HttpContext.Current.Session["CompCode"], 0);
            dt = oAccount.GetIteminfo(prefixText);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    itemList.Add(new Iteminfo() { Name = dt.Rows[i]["groupname"].ToString() + dt.Rows[i]["subgroup"].ToString() + dt.Rows[i]["itemname"], Title = dt.Rows[i]["itemname"].ToString(), 
                    value = dt.Rows[i]["groupname"].ToString() + dt.Rows[i]["subgroup"].ToString() + dt.Rows[i]["itemname"].ToString()+";"+dt.Rows[i]["itemcode"].ToString() });
                }
            }
        }
        catch (SqlException)
        {
            itemList.Add(new Iteminfo() { Name = "Problem Getting Results.", value = "Error" });
        }

        if (itemList.Count == 0)
            itemList.Add(new Iteminfo() { Name = "Nothing to Display.", value = "Info" });

        return itemList;
    }
}

页面方式:

[WebMethod]
public List<Iteminfo> FetchItemList(string prefix)
{
    var items = new Iteminfo();
    var fetchitems = items.GetItems(prefix);
    //.Where(m => m.Name.ToLower().StartsWith(prefix.ToLower()));

    // .Where(m => m.Name.ToLower().StartsWith(prefix.ToLower()));
    return fetchitems.ToList();
}
4

1 回答 1

0

您的第一个问题似乎在SearchItem起作用。您不应该将所有这些更改item为,item1因为这是内部 jQuery UI 名称并且它始终相同(对于所有实例)。所以你的 JavaScript 函数应该是这样的:

function SearchItem() {
    $('input[name$="txtitem"]').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "Itemslist.asmx/FetchItemList",
                data: "{ 'prefix': '" + request.term + "' }",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function(data) {
                    response(data.d);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });
        },
        minLength: 1,
        focus: function(event, ui) {
            $('input[name$="txtitem"]').val(ui.item.Name);
            return false;
        },
        select: function(event, ui) {
            $('input[name$="txtitem"]').val(ui.item.Name);
            $('input[name$="hditem"]').val(ui.item.value);
            return false;
        }
    }).data('autocomplete')._renderItem = function(ul, item) {
        return $('<li>').data('item.autocomplete', item).append('<a>' + item.Name + '</a>').appendTo(ul);
    };
}

可能还有一些关于选择器的进一步错误,但如果没有 HTML/ASPX 代码,这是无法判断的。

于 2012-10-18T11:55:37.407 回答