如果用户输入了错误的值,如何在自动完成下获得控件

时间:2013-02-20 09:30:10

标签: asp.net jquery autocomplete

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

<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)的功能),我需要突出显示文本框。如何执行此操作?

1 个答案:

答案 0 :(得分: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) {
            }
    });
}