Вопрос

i am using $jquery ajax method to fatch data and display in 3 fields, i used same ajax method for 3 different textboxs , To, CC, Bcc, and i want to use 1 ajax method to fatch data , here tagSource : one function to call ajax method

jQuery(document).ready(function () {
            jQuery("#totags").tagit({
                allowSpaces: true,
                tagSource: function (request, response) {
                    $.ajax({
                        type: "POST",
                        url: "Blog.aspx/GetName",
                        data: "{'match': '" + request.term + "'}",
                        dataType: "json",
                        contentType: "application/json",
                        success: function (data) {
                            console.log(data);
                            if (data.d != null) {
                                response($.map(data.d, function (item) {
                                    return {
                                        label: item.Name,
                                        value: item.id,
                                    };
                                }));
                            }
                        }
                    });
                },
            });
            jQuery("#cctags").tagit({
                allowSpaces: true,
                tagSource: function (request, response) {
                    $.ajax({
                        type: "POST",
                        url: "Blog.aspx/GetName",
                        data: "{'match': '" + request.term + "'}",
                        dataType: "json",
                        contentType: "application/json",
                        success: function (data) {
                            console.log(data);
                            if (data.d != null) {
                                response($.map(data.d, function (item) {
                                    return {
                                        label: item.Name,
                                        value: item.id,
                                    };
                                }));
                            }
                        }
                    });
                },
            });
            jQuery("#bcctags").tagit({
                allowSpaces: true,
                tagSource: function (request, response) {
                    $.ajax({
                        type: "POST",
                        url: "Blog.aspx/GetName",
                        data: "{'match': '" + request.term + "'}",
                        dataType: "json",
                        contentType: "application/json",
                        success: function (data) {
                            console.log(data);
                            if (data.d != null) {
                                response($.map(data.d, function (item) {
                                    return {
                                        label: item.Name,
                                        value: item.id,
                                    };
                                }));
                            }
                        }
                    });
                },
            });
        });
Это было полезно?

Решение

You can chain selectors using jQuery:

jQuery(document).ready(function () {
    jQuery("#totags, #cctags, #bcctags").tagit({
        // ..
    });
});

Другие советы

in general case assign it to var:

var ajaxcall=function(request, response) {
    $.ajax({
        type: "POST",
        url: "Blog.aspx/GetName",
        data: "{'match': '" + request.term + "'}",
        dataType: "json",
        contentType: "application/json",
        success: function(data) {
            console.log(data);
            if (data.d != null) {
                response($.map(data.d, function(item) {
                    return {
                        label: item.Name,
                        value: item.id,
                    };
                }));
            }
        }
    });
};​

       jQuery("#totags").tagit({
            allowSpaces: true,
            tagSource: ajaxcall
        });
        jQuery("#cctags").tagit({
            allowSpaces: true,
            tagSource: ajaxcall
        });
        jQuery("#bcctags").tagit({
            allowSpaces: true,
            tagSource: ajaxcall
        });

in particular case use chain as Florent answered.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top