我写了一个插件,它在后端有一个用户管理页面。此页面通过AJAX调用从数据库加载用户数据。此函数返回的数据确实包含对javascript库进行类引用的HTML。
对于我的usermanagement页面,我已将所有必需的javascript库排队,但它们无法处理AJAX调用加载的数据:
functions.php
...
if ( $_GET[\'page\'] == \'test_usermanagement\' ) {
wp_register_script(\'usermanagement\', plugins_url(\'/js/usermanagement.js\', __FILE__), array(\'jquery\') );
wp_enqueue_script(\'usermanagement\', plugins_url(\'/js/usermanagement.js\', __FILE__), array(\'jquery\') );
wp_register_script(\'usermanagement_select\', plugins_url(\'/js/select2.min.js\', __FILE__), array(\'jquery\') );
wp_enqueue_script(\'usermanagement_select\', plugins_url(\'/js/select2.min.js\', __FILE__), array(\'jquery\') );
}
...
add_options_page(
\'User Management\',
\'User Management\',
\'manage_options\',
\'test_usermanagement\',
\'test_usermanagement_page\'
);
...
function test_usermanagement_page() {
...
}
...
function test_loadUsers() {
echo "<select name=\'user_select[]\' class=\'usermanagement_select\' multiple=\'multiple\'>";
$ajaxdata_users_select = $wpdb->get_results("SELECT id as ...
$count = 0;
$foreach($ajaxdata_users_select as userdata) {
echo "<option value=\'".$ajaxdata_users_select[$count]->user_id ."</option>";
...
echo "</select>";
}
usermanagement.js
jQuery(document).ready(function($) {
$(\'.usermanagement_select\').select2();
});
...
var usersStartIndex = 0;
$.ajax({
type: \'POST\',
url: ajaxurl,
data: {
action: \'test_loadUsers\',
$_usersStartIndex: usersStartIndex,
},
success: function(html){
console.log(\'success\');
$(\'#results\').html(html);
}
});
不幸的是,通过这种方式,库“select2”无法在我的选择框上工作。如果我将选择框直接放入函数mpbs\\u usermanagement2\\u page(),它会这样做。
我要做什么才能让它工作?
提前谢谢。问候Lars