select2:ajax和自定义坐标[lon,lat]标记

select2 : ajax and custom coordinates [lon,lat] tags

本文关键字:lon lat 标记 坐标 ajax 自定义 select2      更新时间:2023-09-26

我正在使用select2构建一个地理搜索框。

用户应该能够输入城市、国家或十进制坐标[lon,lat]。通过ajax调用在服务器上查询城市/国家

当检测到输入"["时,是否可以阻止ajax调用,并且仅当lon、lat与下面的regex匹配时才创建搜索选项?

^[-+]?(180('.0{1,15})?|((1[0-7]'d)|([1-9]?'d))('.'d{1,15})?),[-+]?([1-8]?'d('.'d{1,15})?|90('.0{1,15})?)$

如果用户试图在不匹配正则表达式的情况下关闭带有"]"的[lon,lat]标记,它应该显示一条自定义消息

我当前没有坐标标签的代码是:

    $('.select2-search').select2({
        placeholder: 'Country, city or [lon,lat]',
        multiple: true,
        minimumInputLength: 2,
        maximumSelectionSize: 4,
        ajax: {
            url: "/citiesCountries",
            dataType: 'json',
            quietMillis: 100,
            data: function(term) {
                return {
                    search: term
                };
            },
            results: function(data) {
                return { results: data}
            }
       }
    });

提前感谢

您应该使用传输,例如:

$('.select2-search').select2({
    placeholder: 'Country, city or [lon,lat]',
    multiple: true,
    minimumInputLength: 2,
    maximumSelectionSize: 4,
    ajax: {
        url: "/citiesCountries",
        dataType: 'json',
        quietMillis: 100,
        data: function(term) {
            return {
                search: term
            };
        },
        results: function(data) {
            return { results: data}
        },
        transport: function(params){
            params.beforeSend = function(request){
                alert('your regex condition here');
            };
            return $.ajax(params);
        }
   }
});