无法将jQuery放在加载的document.ready中:未调用

时间:2015-03-20 13:50:39

标签: javascript jquery x-editable

我有一个名为:contact_profile.php的页面,它使用jquery .load将信息加载到div中。

$('#json_comments').load('json_comments_content.php');

json_comments_content.php看起来像这样:

<?php
require_once 'core/init.php';

$user = new User();
if(!$user->isLoggedIn()) {
    Redirect::to('login.php');
}

$contact = new Contact();

//check to make sure there is current contact selected, otherwise redirect to index. This helps when you deselect a contact from menu bar while on contact page.
if (!($contact->isSelected())) {
    Redirect::to('index.php');
}

?>
<div class="items general-item-list">
    <div class="item">
        <div class="item-head">
            <div class="item-details">
                <img class="item-pic" data-field="pic_url" src="">
                <a href="" class="item-name primary-link" data-field="username"></a>
                <span class="item-label" data-field="datetime" data-value="" data-pk=""></span>
            </div>
        </div>
        <div class="item-body"><a href="#" id="comment" class="can_possibly_edit" data-type="textarea" data-field="comment" data-pk="" data-value="" data-placement="right" data-original-title="Enter a comment."></a></div>
    </div>
</div> 
    <a href="#" class="items-load">Load More...</a> 

<script type="text/javascript">     

$(document).ready(function() {

    $('.comments_data').loadmore({
        source: 'json_comments.php',
        step: 15,
        userid: '<?php echo $user->data()->id; ?>'
    });

    //on load, disabled the comments editable info on page load so it looks better.
    $('#json_comments a').attr("data-disabled", "true");


    $.fn.editable.defaults.mode = 'inline';


});

</script>

我正在使用名为&#39; loadmore&#39;的自定义插件这将在我的页面上从mysql数据库加载更多数据。它工作正常。

但是,我必须使用以下代码来处理loadmore插件提供的数据:

                $('.edit_me').editable({
                emptytext: ".....",
                url: "ajax_xeditable_update.php?table=comments",
            });

该代码正在使用jQuery的X-Editable插件:http://vitalets.github.io/x-editable/

如果我将代码放在document.ready函数内的已加载内容页面中,它永远不会被调用!

这是我的loadmore插件的样子。如果放置X-Editable的代码,它将正常工作。将代码放在加载的页面而不是插件中会更好 - 这样插件可以保持通用。

希望我能清楚我的问题。

这是loadmore自定义插件:

(function ($) {
    "use strict";

    $.fn.loadmore = function(options) {
            var self = this,

            settings = $.extend({
                source: '',
                step: 2,
                userid: '',

            }, options),

            stepped = 1,
            item = self.find('.item'),
            items = self.find('.items'),

            finished = function() {
                // hide the load more button
                self.find('.items-load').remove();
            },

            append = function(value) {
                var name, part, id, userid, canedit;

                item.remove();

                for(name in value) {
                    if(value.hasOwnProperty(name)) {

                        id = value['id'];
                        userid = value['user_id'];
                        part = item.find('*[data-field="' + name +'"]');
                        //find anything that has a can edit class and then add the general editable class for x-editable to work. 
                        canedit = item.find(".can_possibly_edit");

                        if(part.length){

                             part.text(value[name]);

                            //add the value to an image if there is one for x-editable to work.
                            if($(part).is("img")) {
                                 $(part).attr("src", value[name]);
                             }

                            //only make the current user's stuff editable
                            if(settings.userid == userid ) {
                                //add edit_me to the classes so x=editable can work.   but then remove edit_me and the editable class so x-editable doesn't work for data that doesn't belong to the user(found in the else clause below).
                                $(canedit).addClass('edit_me editable editable-pre-wrapped editable-click editable-disabled');
                                $(canedit).attr('data-value', value[name]);
                                //there must be an id field in the json so it can be assigned to the primary key for x-editable to work.
                                $(canedit).attr('data-pk', id);

                            } else {
                                //remove hyperlink stuff and just leave the text to view only.
                                $(canedit).removeClass('edit_me editable');
                            }

                        }
                    }
                }

                item.clone().appendTo(items);

                //this works if it's placed here only!  

                $('.edit_me').editable({
                    emptytext: ".....",
                    url: "ajax_xeditable_update.php?table=comments",
                });

            },

            load = function(start, count) {
                $.ajax({
                    url: settings.source,
                    type: 'get',
                    dataType: 'json',
                    data: {start: start, count: count},
                    success: function(data) {
                        var items = data.items;

                        if(items.length) {
                            $(items).each(function(index, value) {
                                append(value);
                            });

                            stepped = stepped + count;
                        }

                        if(data.last === true) {
                            finished();
                        }
                    }
                });

            };

        if(settings.source.length) {
            self.find('.items-load').on('click', function(){
                load(stepped, settings.step);
                return false;
            });

            load(1, settings.step);
        } else {
            console.log('Source required for loadmore.');
        }

    };

}(jQuery))

它几乎就像在加载的页面上一样:json_comments_content.php我需要在document.ready上运行loadmore插件,然后在loadmore完成后运行,返回页面并运行:

$('.edit_me').editable({
                emptytext: ".....",
                url: "ajax_xeditable_update.php?table=comments",
            });

不确定是否重要,但loadmore脚本包含在我的主页上:&#39; js / loadmore.js&#39;。它位于子目录中。

0 个答案:

没有答案