获取jQuery自动完成源函数

时间:2015-09-25 19:07:05

标签: javascript jquery

我有一个webservice方法,它接受一个元素的id来确定自动复制的源。

简而言之,我这样做:

$("input[type='text']").autocomplete({
    source: function(request, response) {
        var id = $(this).attr('id');
        var params = {'id': id, 'term': request.term};
        var jsonParams = JSON.stringify(params);
        $.ajax({
            type: "POST",
            url: "Page.aspx/GetAutoCompleteList",
            data: jsonParams,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                response(JSON.parse(msg.d));
            },
            error: function() {
                response([]);
            }
        });
    }
});

id并未引用原始选择器。

如何获取所选input元素的ID?或者对此更好的策略是什么?

2 个答案:

答案 0 :(得分:2)

您需要维护每个输入元素的上下文,如下所示:

$("input[type='text']").each(function (i, ele) {
    ele = $(ele);
    ele.autocomplete({
        source: function (request, response) {
            var id = ele.attr('id');
            var params = {'id': id, 'term': request.term};
            var jsonParams = JSON.stringify(params);
            $.ajax({
                type: "POST",
                url: "Page.aspx/GetAutoCompleteList",
                data: jsonParams,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // ...
                },
                error: function () {
                    // ...
                },
                async: false
            });
        }
    });
});

答案 1 :(得分:1)

尝试

    res {WorldCurrencies.CurrencyTypes} WorldCurrencies.CurrencyTypes
    AED "United Arab Emirates Dirham"   string
    AFN "Afghan Afghani"    string
    ALL "Albanian Lek"  string
    AMD "Armenian Dram" string
    ANG "Netherlands Antillean Guilder" string
    AOA "Angolan Kwanza"    string
    ARS "Argentine Peso"    string
    AUD "Australian Dollar" string
    AWG "Aruban Florin" string
    AZN "Azerbaijani Manat" string
    BAM "Bosnia-Herzegovina Convertible Mark"   string
    BBD "Barbadian Dollar"  string
    BDT "Bangladeshi Taka"  string
    BGN "Bulgarian Lev" string
    ...

JSFIDDLE

$(this.element).prop("id");

this.element[0].id;

$(this.element.get(0)).attr('id');
$("input[type='text']").autocomplete({
    source: function(request, response) {
        var id = $(this.element).prop("id");
        var id2=this.element[0].id;
        var id3=$(this.element.get(0)).attr('id');
        console.log(id);
        console.log(id2);
        console.log(id3);
        var params = {'id': id, 'term': request.term};
        var jsonParams = JSON.stringify(params);
        $.ajax({
            type: "POST",
            url: "Page.aspx/GetAutoCompleteList",
            data: jsonParams,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                response(JSON.parse(msg.d));
            },
            error: function() {
                response([]);
            }
        });
    }
});