私はselect-chainプラグインを使用しており、別の選択ボックスに基づいて2番目の選択ボックスを設定するために次のコードを持っています:
$('#rating_state').selectChain({ target: $("#rating_county"), url: 'scripts/chain.php', data: { ajax: true }, selected: '<?php echo $actual_rating_county?>'}).trigger("change");
私の問題は、ページが最初に読み込まれるときに、スクリプトの実行が完了する前に select2 プラグインをトリガーし、結果として選択ボックスが実際に空白になることです。クリック時に以下が実行されるように設定すると、クリック時に問題が修正されます。
$('#rating_county').select2();
JavaScript全体は以下のとおりです。
$(document).ready(function() {
$('#rating_state').selectChain({ target: $("#rating_county"), url: 'scripts/chain.php', data: { ajax: true }, selected: '<?php echo $actual_rating_county?>'}).trigger("change");
$('.select2').select2({
placeholder: "Select an option",
allowClear: true
});
$('#rating_state').change(function(){
$('#rating_county').select2();
});
});