duanque2413 2017-08-05 20:16
浏览 366

jQuery ajax成功方法无法正常工作

I developed a custom widget for the Elementor plugin called carousel slider. There I set elementor default setting field select2 for multiple select. I populate the Elementor Libray templates in that select2 field. Normally works fine. But I want to add newly created template which is created from editor preview iframe. And here the templates are created using ajax method which not loading the page.

To add the newly created item I used Elementor JS hook elementor.hooks.addAction( 'panel/open_editor/widget/widget-type which is fired when any widget is going to be edited or editor of that widget-type is opened. Using jQuery ajax I managed to add the newly created template to that select2 field. But the problem is "When I first time click on that widget-type ( in my case carousel slider ) in preview iframe and opened the editor, the newly created template added and appears in select2 options value. Now I close that editor and again I open editor of my widget by clicking on carousel widget the new added template disappear. I can not figured out the problems. My codes are below..

(function( $ ) {
    $(document).ready( function() {
        elementor.hooks.addAction( 'panel/open_editor/widget/premium-carousel-widget', function( panel, model, view ) {
            $.ajax({
                url : template_update.ajax_url,
                type : 'POST',
                data : {
                    action : 'update_template',
                    whatever : 'yes'
                },
                success : function( data ) {
                    if( data !== '' ) {
                        console.log( data );
                        $('select[data-setting=premium_carousel_slider_content]').append( $(data ) );
                    }
                    console.log( 'hello' );
                }
            });
        });
    });
})(jQuery);

PHP codes are below. I used WordPress ajax API.

public function updating_template_after_save() {
    if( $_POST['whatever'] ) {
        $pagelist = get_posts(array(
            'post_type' => 'elementor_library',
            'showposts' => 999,
        ));

        $options = array();
        if ( ! empty( $pagelist ) && ! is_wp_error( $pagelist ) ){
            foreach ( $pagelist as $post ) {
                $options[ $post->ID ] = __( $post->post_title, 'premium_elemnetor' );
            } 
        }

        $old_items = get_option( 'temp_count' );
        if( $old_items != $options ) {
            $html = '';
            $new_item = array_diff( $options, $old_items );
            foreach( $new_item as $key => $value ) {
                $html .= '<option value="'.$key.'">'. $value .'</option>';
            }
            echo $html;
            update_option( 'temp_count', $options );
        }
    }
    die();
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了