admin管理员组文章数量:1426086
I have written a plugin which has a usermanagement page in the backend. This page loads userdata from the database by an AJAX call. The data returned from this function does contain HTML with class references to a javascript library.
For my usermanagement page I have enqueued all required javascript libraries, but they do not work on the data loaded by the AJAX call:
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);
}
});
Unfortunately this way the library "select2" is not working on my select box. If I put the select box directly into the function mpbs_usermanagement2_page(), it does.
What do I have to get it working?
Thanks in advance. Regards Lars
I have written a plugin which has a usermanagement page in the backend. This page loads userdata from the database by an AJAX call. The data returned from this function does contain HTML with class references to a javascript library.
For my usermanagement page I have enqueued all required javascript libraries, but they do not work on the data loaded by the AJAX call:
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);
}
});
Unfortunately this way the library "select2" is not working on my select box. If I put the select box directly into the function mpbs_usermanagement2_page(), it does.
What do I have to get it working?
Thanks in advance. Regards Lars
Share Improve this question edited May 23, 2019 at 15:39 Gardinero asked May 23, 2019 at 15:30 GardineroGardinero 1011 bronze badge 3 |1 Answer
Reset to default 0So, the success function in the ajax function has to look like the following:
success: function(html){
console.log('success');
$( '#results' ).html(html);
$( '#results .usermanagement_select' ).select2({tags: true});
}
本文标签: AJAX call of function containing javascript which is not loaded (Plugin development)
版权声明:本文标题:AJAX call of function containing javascript which is not loaded (Plugin development) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745466293a2659548.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
success: function(html){
You need to run.select2()
on any new items in the resuts. – Jacob Peattie Commented May 24, 2019 at 14:49