Javascript-更改select2中的字符

Javascript-更改select2中的字符,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,我使用select2选择不同的地区和国家。我想在select2中使用一些方法,在我检查数据之前更改特定字符。这是我的初始代码: var initSearchSelect2 = function (className) { $(className).select2({ language: "en", allowClear: true, data: [ { id: 1,

我使用select2选择不同的地区和国家。我想在select2中使用一些方法,在我检查数据之前更改特定字符。这是我的初始代码:

var initSearchSelect2 = function (className) {
    $(className).select2({
        language: "en",
        allowClear: true,
        data: [
            {
                id: 1,
                text: 'All regions',
                parent: 0,
                children: $scope.regions // here is my data
            }
        ],
        escapeMarkup: function (m) {
            return m;
        },
        initSelection: function (element, callback) {
            var userNeighborhood = $cookies.get("neighborhood");
            if (!isUndefinedOrNull(userNeighborhood)) {
                userNeighborhood = userNeighborhood.split(',');
                var initialVal = {
                    'city': userNeighborhood[0],
                    'text': userNeighborhood[1],
                    }
                callback(initialVal);

            }
            else {
                callback({id: 0, text: 'check it'})
            }
        },
        placeholder: "select your region ..."
    }, true);
}
我想检查用户是否搜索伦敦,我在$scope.regions中检查英格兰而不是伦敦。我曾经在select2中检查查询和匹配器,如下所示:

query: function(options) {
    console.log(options);
    if(options.term) {
        options.term = options.term.replace('London','England');
    }

    return options
},

然而,我还没有得到任何有趣的结果。有什么解决的建议吗

您可以覆盖数据方法:

$ajax.select2({
    ajax: {
      url: "https://api.github.com/search/repositories",
      dataType: 'json',
      delay: 250,
      data: function (params) {
        return {
          q: 'test', //put your replacement here
          page: params.page
        };
      }
    }
});
活生生的例子。无论您输入什么,测试都将始终发送到服务器