我遇到了选择 2 的问题,其中第二个选择 2 从 AJAX 中拉出,它返回要替换的 HTML。
主要问题似乎是第二个 select 2 'select' 没有添加选定的属性,因为某些上帝遗弃的原因...... console.log 没有启动。
function select2_format_flag(state) {
var originalOption = state.element;
if (!$(originalOption).data('iso')) return state.text;
return "<img class='flag' src='<?=$adtrafik_config['uri_assets']?>img/flag/" + $(originalOption).data('iso') + ".gif' /> " + state.text;
}
function select2_format_engine(state) {
var originalOption = state.element;
if (!$(originalOption).data('engine')) return state.text;
return "<img class='engine' src='<?=$adtrafik_config['uri_assets']?>img/img/engine/" + $(originalOption).data('engine') + ".png' /> " + state.text;
}
jQuery(document).ready(function () {
//API Path
var adtrafik_api = '<?=$adtrafik_config['
uri_post '].'
seo / report - regional / api / '?>';
//Select2
$('#go').attr('disabled', 'disabled');
$('#d_to').attr('disabled', 'disabled');
$('#d_from').attr('disabled', 'disabled');
//Init. Select2
$('#d_engine').select2({
placeholder: "Select Engine...",
allowClear: true,
width: 'resolve',
formatResult: select2_format_engine,
formatSelection: select2_format_engine
});
$("#d_market").select2({
width: 'resolve',
formatResult: select2_format_flag,
formatSelection: select2_format_flag
});
$('#go').click(function (event) {
event.preventDefault();
var d_from = $('#d_from').val();
var d_to = $('#d_to').val();
var data_key_engine = $("#d_engine option:selected").attr('data-engine').toLowerCase();
var engineid = data_key_engine;
//var engineid = data_key_market + '-' + data_key_engine;
<? php
if (isset($uri_page[2])) { ?>
window.location.href = '<?php echo $adtrafik_config['
uri_post '].'
seo / report - regional / overview / '; ?>' + engineid + '/' + d_from + '/' + d_to + '/'; <? php
} else { ?>
window.location.href = '<?php echo $adtrafik_config['
uri_post '].'
seo / report - regional / '.$uri_page[2].' / '; ?>' + engineid + '/' + d_from + '/' + d_to + '/'; <? php
} ?>
});
//Add Formatter and AJAX to Market
$('#d_market').change(function () {
var id = $('#d_market').val();
var dataString = 'type=nav&call=d_engine&d_market=' + id;
var data_key_market = $("#d_market option:selected").attr('data-iso').toLowerCase();
$.ajax({
type: 'GET',
url: adtrafik_api,
data: dataString,
cache: false,
success: function (html) {
$('#d_engine').select2('enable');
$('#d_engine').html(html);
}
});
});
$('#d_market').change(function () {
console.log('i changed');
});
$('#d_engine').change(function () {
console.log('i changed too....');
});
//END this crap!
});