功能没有定义,即使它是

时间:2017-07-14 08:50:58

标签: javascript php ajax wordpress undefined-function

我的脚本昨天工作正常,但今天我收到错误:

wldk-kurser-admin.js?ver=4.7.5:13 Uncaught ReferenceError: handleSaveAllDokumenterAction is not defined
    at HTMLDocument.<anonymous> (wldk-kurser-admin.js?ver=4.7.5:13)
    at i (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,wp-ajax-response,plupload&ver=4.7.5:2)
    at Object.fireWith [as resolveWith] (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,wp-ajax-response,plupload&ver=4.7.5:2)
    at Function.ready (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,wp-ajax-response,plupload&ver=4.7.5:2)
    at HTMLDocument.K (load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils,wp-ajax-response,plupload&ver=4.7.5:2)

这是我的剧本

(function ($, context) {
$(document).ready(function () {

    // makers
    listenOnDokumentChangeEvent();
    listenOnDokumentToggleEvent();

    handleAddDokumentAction();
    handleSaveDokumentAction();
    handleSaveAllDokumenterAction();
    handleRemoveDokumentAction();

    // fields
    handleMediaUploadField();
});



function listenOnDokumentChangeEvent() {
    $('.wldk-kurser-dokument').live('keyup change', function (e) {
        // elements
        var $wrapper = $(this);
        var $dokument = $wrapper.find('input[name=dokument_id]');
        var dokumentId = $dokument.val();
        var $saveButton = $wrapper.find('.wldk-kurser-save-dokument');

        // show that some changes have taken place
        if (e.type === 'change') {
            $saveButton.addClass('g1gmap-needs-update');
        }

        // data
        var titel = $wrapper.find('[name=dokument_titel]').val();
        var fil_url =$wrapper.find('[name=dokument_fil_url]').val();



    });
}


function listenOnDokumentToggleEvent () {
    $('.wldk-kurser-dokument-toggle').live('click', function (e) {
        $(this).parents('li').toggleClass('wldk-kurser-dokument-on wldk-kurser-dokument-off');
    });
}

function handleAddDokumentAction() {
    $('.wldk-kurser-add-dokument').click(function (event) {
        event.preventDefault();

        var $wrapper = $(this).parents('#wldk-kurser-new-dokument');
        var $kursus = $wrapper.find('input[name=kursus_id]');
        var $nonce = $wrapper.find('input[name=update_nonce]');
        var $titel = $wrapper.find('input[name=dokument_titel]');
        var $fil_url = $wrapper.find('input[name=dokument_fil_url]');
        var kursus = $kursus.val();
        var titel = $titel.val();
        var fil_url = $fil_url.val();

        if (!titel || !fil_url ) {
            alert(g1_gmap_i18n.required_fields_missing_msg);
            return;
        }


        // ajax call
        var xhr = $.ajax({
            'type': 'POST',
            'url' : ajaxurl,
            'data': {
                'action'   : 'wldk_kurser_add_dokument',
                'security' : $nonce.val(),
                'ajax_data': {
                    'kursus_id': kursus,
                    'titel'   : titel,
                    'fil_url'  : fil_url,
                }
            }
        });

        // success response
        xhr.done(function (response) {
            if (response === '0' && response === '-1') {
                alert(g1_gmap_i18n.add_marker_error_msg);
                return;
            }

            var $res = $(response);

            $res.insertBefore($wrapper);

            $wrapper.find('.wldk-kurser-dokument-toggle').trigger('click');
            $res.find('.wldk-kurser-dokument-toggle').trigger('click');

            handleMediaUploadField();

            // clear fields
            $titel.val($kursus.val());
            $fil_url.val('');


        });
    });
}

function handleSaveDokumentAction() {
    $('.wldk-kurser-save-dokument').live('click', function (event) {
        event.preventDefault();

        // elements
        var $wrapper = $(this).parents('.wldk-kurser-dokument');
        var $saveButton = $wrapper.find('.wldk-kurser-save-dokument');
        var $nonce = $('#wldk-kurser-new-dokument').find('input[name=update_nonce]');

        // data
        var dokumentId = $wrapper.find('input[name=dokument_id]').val();
        var titel = $wrapper.find('input[name=dokument_titel]').val();
        var fil_url = $wrapper.find('input[name=dokument_fil_url]').val();

        // validation
        if (!titel || !fil_url) {
            alert(g1_gmap_i18n.required_fields_missing_msg);
            return;
        }

        // indicate saving action
        $saveButton.addClass('g1gmap-marker-saving');

        // ajax call
        var xhr = $.ajax({
            'type': 'POST',
            'url' : ajaxurl,
            'data': {
                'action'   : 'wldk_kurser_save_dokument',
                'security' : $nonce.val(),
                'ajax_data': {
                    'id'        : dokumentId,
                    'titel'       : titel,
                    'fil_url'      : fil_url,
                }
            }
        });

        // success response
        xhr.done(function (response) {
            $wrapper.find('.wldk-kurser-save-dokument')
                .removeClass('g1gmap-needs-update')
                .removeClass('g1gmap-marker-saving');

            if (response === '0' && response === '-1') {
                alert(g1_gmap_i18n.save_marker_error_msg);
                return;
            }
        });
    });
}

function handleSaveAlldokumenterAction() {
    $('.wldk-kurser-save-all-dokumenter').on('click', function (event) {
        event.preventDefault();

        $('.wldk-kurser-dokument .wldk-kurser-save-dokument').trigger('click');
    });
}

function handleRemovedokumentAction() {
    $('.wldk-kurser-remove-dokument').live('click', function (event) {
        event.preventDefault();

        if (!confirm(g1_gmap_i18n.remove_marker_confirm_msg)) {
            return;
        }

        var $wrapper = $(this).parents('.wldk-kurser-dokument');
        var $dokument = $wrapper.find('input[name=dokument_id]');
        var $nonce = $('#wldk-kurser-new-dokument').find('input[name=update_nonce]');
        var dokumentId = $dokument.val();

        // ajax call
        var xhr = $.ajax({
            'type': 'POST',
            'url' : ajaxurl,
            'data': {
                'action'   : 'wldk_kurser_remove_dokument',
                'security' : $nonce.val(),
                'ajax_data': {
                    'id': dokumentId
                }
            }
        });

        // success response
        xhr.done(function (response) {
            if (response === '0' && response === '-1') {
                alert(g1_gmap_i18n.remove_marker_error_msg);
                return;
            }

            // remove edit form
            $wrapper.remove();
        });
    });
}


function handleMediaUploadField() {
    $('.g1gmap-media-upload-field:not(.g1-events-bound)').each(function () {
        var $field = $(this);
        var $clearButton = $('.g1gmap-clear-button', $field);
        var $value = $('input.g1gmap-media-upload-input', $field);
        var $imagePath = $('.g1gmap-media-upload-image-path', $field);
        var $preview = $('.g1gmap-media-upload-preview', $field);

        $value.val() ? $clearButton.show() : $clearButton.hide();

        $field.addClass('g1gmap-events-bound');

        $('.g1gmap-clear-button', $field).click(function () {
            $value.val('');
            $imagePath.val('');
            $clearButton.hide();
            $preview.empty();
            $(this).trigger('change');

            return false;
        });
    });

    $('.g1gmap-media-upload-button').live('click', function () {
        var $field_wrapper = $(this).parents('.g1gmap-media-upload-field');
        var $input = $field_wrapper.find('.g1gmap-media-upload-input');
        var $imagePath = $field_wrapper.find('.g1gmap-media-upload-image-path');
        var $preview = $field_wrapper.find('.g1gmap-media-upload-preview');
        var $clearButton = $field_wrapper.find('.g1gmap-clear-button');

        var frame = wp.media.frames.file_frame = wp.media(
            {
                title   : 'Select media',
                button  : {
                    text: 'Save'
                },
                multiple: false
            }
        );

        frame.on('open', function () {
            var id = $input.val();

            if (id) {
                var selection = frame.state().get('selection');
                var attachment = wp.media.attachment(id);

                if (attachment) {
                    attachment.fetch();
                }

                if (selection) {
                    selection.add(attachment ? [ attachment ] : []);
                }
            }
        });

        frame.on('select', function () {
            var attachment = frame.state().get('selection').first().toJSON();

            var path = '';
            var $img = $('<img>');

            if (typeof attachment != 'undefined') {
                if (typeof attachment.sizes !== 'undefined' && typeof attachment.sizes.full !== 'undefined') {
                    $img.attr('src', attachment.sizes.full.url);
                    path = attachment.sizes.full.url;
                } else {
                    $img.attr('src', attachment.url);
                    path = attachment.url;
                }
            }

            $input.val(attachment.id);
            $imagePath.val(path);
            $preview.html($img);
            $clearButton.show();

            // in some cases, wp media upload doesn't close properly
            // so we close it manually
            $('.media-modal-close:first').trigger('click');
            $field_wrapper.trigger('change');
        });

        frame.open();

        return false;
    });
}

})(jQuery, window);

我不理解它,因为函数已定义,handleAddDokumentAction和handleSaveDokumentaction不会导致任何问题。我确信它是小而小的,但我无法看到它。我特别困惑,因为它似乎昨天工作得很好。 相关PHP:

 public function ajax_add_dokument() {
        check_ajax_referer( 'wldk-kurser-update-dokumenter', 'security' );

        $ajax_data = $_POST['ajax_data'];
                $meta_data = array(
            'titel'        => '',
            'fil_url'       => '',
        );

        $meta_data['titel'] = $ajax_data['titel'];
        $meta_data['fil_url'] = $ajax_data['fil_url'];

        $post_data = array(
            'post_status' => 'publish',
            'post_type'   => 'wldk_kurser_dokument',
            'post_title' => $meta_data['titel'],
        );

        $post_id = wp_insert_post( $post_data );

        if ( $post_id === 0 ) {
            die();
        }



        update_post_meta( $post_id, '_wldk_kurser_dokument', $meta_data );
        update_post_meta( $post_id, '_wldk_kurser_dokument_titel', 
$meta_data['titel'] );
        update_post_meta( $post_id, '_wldk_kurser_dokument_fil_url',             
$meta_data['fil_url'] );
        update_post_meta( $post_id, '_wldk_kurser_dokument_kursus_id', 
$ajax_data['kursus_id'] );

        echo $this->render_dokument_markup( get_post( $post_id ) );
    }

    public function ajax_remove_dokument() {
        check_ajax_referer( 'wldk-kurser-update-dokumenter', 'security' );

        $ajax_data = $_POST['ajax_data'];

        $post_id = $ajax_data['id'];

        if ( ! $post_id ) {
            die();
        }

        $post = get_post( $post_id );

        if ( $post->post_type === 'wldk_kurser_dokument' ) {
            // our built-in marker can be removed
            $post = wp_delete_post( $post_id, true );

            if ( ! $post ) {
                die();
            }
        } else {
            // we can't remove post, we can only remove its marker metadata

        }

        echo 'Dokument successfully removed.';
    }

    public function ajax_save_dokument() {
        check_ajax_referer( 'wldk-kurser-update-dokumenter', 'security' );

        $ajax_data = $_POST['ajax_data'];

        $post_id = $ajax_data['id'];

        unset( $ajax_data['id'] );

        if ( ! $post_id ) {
            die();
        }

        $post = get_post( $post_id );


        update_post_meta( $post_id, '_wldk_kurser_dokument', $ajax_data );
        update_post_meta( $post_id, '_wldk_kurser_dokument_titel', 
$ajax_data['titel'] );
        update_post_meta( $post_id, '_wldk_kurser_dokument_fil_url', 
$ajax_data['fil_url'] );

        echo 'Dokument successfully updated.';
    }
    public function wldk_kurser_dokumenter_meta_box(){
// Add an nonce field so we can check for it later.
        wp_nonce_field( 'wldk_kurser_dokumenter_meta_box', 
'wldk_kurser_dokumenter_meta_box_nonce' );
/*
        // post stored data
        $values = get_post_meta( $post->ID, '_g1_gmap', true );

        // defined defaults
        $defaults = $this->get_map_fields_defaults( 
'g1_gmaps_map_markers_meta_box' );

        // apply filter on defaults
        $defaults = apply_filters( 'g1_gmaps_map_markers_defaults', 
$defaults );

        // merge values from the database with defaults (on first load in db 
there are no values)
        $values = wp_parse_args( $values, $defaults );
*/
        $args = array(
            'meta_key'   => '_wldk_kurser_dokument_kursus_id',
            'meta_value' => $post->ID,
            'post_type'  => 'wldk_kurser_dokument',
            'orderby'    => 'ID',
            'order'      => 'ASC',
            'posts_per_page' => - 1,
        );

        $dokumenter = get_posts( $args );

        // new marker
        $ajax_nonce = wp_create_nonce( 'wldk-kurser-update-dokumenter' );
        ?>

        <ul class="wldk-kurser-dokumenter">
            <?php foreach ( $dokumenter as $dokument ): ?>
                <?php echo $this->render_dokument_markup( $dokument ); ?>
            <?php endforeach; ?>

            <li class="wldk-kurser-dokumenter-new wldk-kurser-dokument-off" 
id="wldk-kurser-new-dokument">
                <div class="wldk-kurser-dokument-toggle"><?php echo _e( 
'Tilføj nyt dokument', 'g1_gmaps' ); ?></div>
                <div class="wldk-kurser-dokument-content">
                    <table class="form-table">
                        <tbody>
                            <tr valign="top">
                                <th valign="top">
                                    <label>Titel<span class="required">*
</span></label>
                                </th>
                                <td>
                                    <input data-g1-label-for="new-marker" 
type="text" name="dokument_titel" value="" /><br />
                                </td>
                            </tr>
                            <tr valign="top">
                                <th scope="row">
                                    <label>Dokument<span class="required">*
</span></label>
                                </th>
                                <td>
                                    <input data-g1-label-for="new-marker" 
type="text" name="dokument_fil_url" value="" /><br />
                                </td>
                            </tr>
                        </tbody>
                    </table>

                    <p class="wldk-kurser-dokument-actions">
                        <a href="#" class="button button-small wldk-kurser-
add-dokument">Tilføj</a>
                        <input type="hidden" name="update_nonce" value="<?
php echo esc_attr( $ajax_nonce ); ?>" />
                        <input type="hidden" name="kursus_id" value="<?php 
echo esc_attr( $post->ID ); ?>" />
                    </p>
                </div>
            </li>
        </ul>

        <p class="wldk-kurser-dokumenter-actions">
            <a href="#" class="button wldk-kurser-save-all-dokumenter">Gem 
alle Dokumenter</a>
        </p>
    <?php
    }
protected function render_dokument_markup( $post ) {
        $id = $post->ID;

        $values = get_post_meta( $id, '_wldk_kurser_dokument', true );

        $marker_type_label = '';

        if ( $post->post_type !== 'wldk_kurser_dokument' ) {
            $obj               = get_post_type_object( $post->post_type );
            $marker_type_label = ' (' . $obj->labels->singular_name . ')';
        }
        ?>
        <li class="wldk-kurser-dokument wldk-kurser-dokument-off">
            <div class="wldk-kurser-dokument-toggle"><?php echo esc_html( 
$values['titel'] ); ?></div>
            <div class="wldk-kurser-dokument-content">
                <table class="form-table">
                    <tbody>
                        <tr valign="top">
                            <th scope="row">
                                <label>Titel</label>
                            </th>
                            <td>
                                <input type="text" name="dokument_titel" 
value="<?php echo $values['titel']; ?>" /><?php echo esc_html( 
$marker_type_label ); ?>
                            </td>
                        </tr>
                        <tr valign="top">
                            <th scope="row">
                                    <label>Dokument<span class="required">*</span></label>
                                </th>
                                <td>
                                    <input data-g1-label-for="new-marker" type="text" name="dokument_fil_url" value="<?php echo $values['fil_url']; ?>" /><br />
                                </td>
                        </tr>
                    </tbody>
                </table>

                <input type="hidden" name="dokument_id" value="<?php echo esc_attr( $id ); ?>" />

                <p class="wldk-kurser-dokument-actions">
                    <a href="#" class="button button-small button-primary wldk-kurser-save-dokument">Gem</a>
                    <a href="#" class="button button-small wldk-kurser-remove-dokument">Slet</a>
                </p>

            </div>
        </li>
    <?php
    }

2 个答案:

答案 0 :(得分:1)

你有错误的错误:

handleSaveAlldokumenterAction的块替换为以下内容:

function handleSaveAllDokumenterAction() {
$('.wldk-kurser-save-all-dokumenter').on('click', function (event) {
    event.preventDefault();

    $('.wldk-kurser-dokument .wldk-kurser-save-dokument').trigger('click');
});
}

答案 1 :(得分:0)

var test=function(){
  alert('here');
}
(function ($, context) {
  $(document).ready(function () { 
      test();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

调用时未定义您的功能。 尝试使用全局变量定义函数,并可以从任何位置调用它。然后不会出现未定义的问题