3

私のページには次の選択があります。

<select><option value="1" selected="selected">Caption</option></select>

select2 (v 4.0) init を呼び出します。

city.select2({
    ajax: {
        url: <...>,
        data: <...>,
        processResults: <...>,
        cache: true
    },
    escapeMarkup: function(markup){ return markup; },
    minimumInputLength: 0,
    templateResult: function(repo){ return repo.name; },
    templateSelection: function(repo){ return repo.name; }
});

問題は、select2 がデフォルトの選択値をリセットし、空白の文字列を表示していることです。select2 init でデフォルト値を設定する方法はありますか?

4

2 に答える 2

5

プロパティがデータオブジェクトにあるとtemplateSelection予想しているため、問題はメソッドにありました。現在必須であり、再マップした場合にメソッドが不要になるnameという事実は別として、データ オブジェクトにプロパティがある場合を処理していません。texttext

city.select2({
    ajax: {
        url: <...>,
        data: <...>,
        processResults: <...>,
        cache: true
    },
    escapeMarkup: function(markup){ return markup; },
    minimumInputLength: 0,
    templateResult: function(repo){ return repo.name || repo.text; },
    templateSelection: function(repo){ return repo.name || repo.text; }
});

これで問題が解決し、最初の選択が正しく表示されるはずです。

于 2015-05-20T15:12:01.470 に答える