【问题标题】:redirect to URL from select2 drop down从 select2 下拉菜单重定向到 URL
【发布时间】:2016-10-05 17:26:22
【问题描述】:

我正在使用 Select2 (https://select2.github.io)。

我想使用 select2 作为实时 ajax 搜索输入。

所以我尝试做到:

var $similar = $(".datajax-items");

$similar.select2({

    placeholder: "search...",
    maximumSelectionSize: 1,
    ajax: {
           ...
        },
        processResults: function(data, params) {
            params.page = params.page || 1;

            return {
                results: data.items,
                pagination: {
                    more: (params.page * 20) < data.total_count
                }
            };
        },
        cache: true
    },
    escapeMarkup: function(markup) { return markup; },
    templateResult: formatRepo,
    templateSelection: formatRepoSelection
});

和功能:

function formatRepo(repo) {
if (repo.loading) return repo.text;

var markup = "<a href=\"google.com\" class='select2-result-repository clearfix'></a>";

return markup;
}

function formatRepoSelection(repo) {
    return repo.id || repo.text;
}

代码有效。但是当我单击一个选项(在选择框中)时,它(通常)选择它。我不想选择它。我想禁用选择,然后将页面重定向到自定义 URL。

【问题讨论】:

    标签: jquery ajax select2


    【解决方案1】:

    我找到了一个简单的解决方案:

    声明一个变量(例如服务器端(json)中的“url”)并将代码更改为:

    function formatRepoSelection(repo) {
        if(repo.url)  
        {
            window.location.replace(repo.url);
            return 'please wait...';
        } 
        return repo.title || repo.text;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 2018-08-18
      • 1970-01-01
      • 2018-03-28
      相关资源
      最近更新 更多