1

我有一个表格,其中每一行都有一个具有自动完成功能的文本框。在服务器代码中,它看起来像

<td><asp:TextBox ID="tbNumber" runat="server" onfocus="AttachNumbers(this)" /></td>

在客户端

function AttachNumbers(sender) {
    $(sender).autocomplete({ source: GetNumbers, minLength: 2 });
}

function GetNumbers(request, response) {
    $.ajax({
        type: "POST",
        url: AJAXSERVICEMETHOD,
        data: "{ 'term': '" + request.term + "', 'count': 10 }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success:
            function(data, textStatus, jqXHR) {
                response(data.d);
                //HighlightError(HOW TO GET CONTROL HERE?);
            },
        error:
            function(jqXHR, textStatus, errorThrown) {
            }
    });
}

如果用户没有从列表中选择数字,我需要的是突出显示文本框(使用我拥有的功能HighlightError(controlid))如何做到这一点?

4

1 回答 1

1

您可以使用 jQuery.proxy作为上下文传递sender给函数:GetNumbers

function AttachNumbers(sender) {
    var sourceProxy = $.proxy(GetNumbers, sender);
    $(sender).autocomplete({ source: sourceProxy, minLength: 2 });
}

function GetNumbers(request, response) {
    var target = this;
    $.ajax({
        type: "POST",
        url: AJAXSERVICEMETHOD,
        data: "{ 'term': '" + request.term + "', 'count': 10 }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success:
            function(data, textStatus, jqXHR) {
                response(data.d);
                // target refers to sender control
                //HighlightError(HOW TO GET CONTROL HERE?);
            },
        error:
            function(jqXHR, textStatus, errorThrown) {
            }
    });
}
于 2013-02-20T10:00:07.423 回答