Why is 'pageinit' firing twice?

喜你入骨 提交于 2019-12-11 03:14:41

问题


Please could someone tell me why the pageinit is running twice? I had an issue where my listview items would be duplicated and by putting in an alert box I identified that it is because the pageinit is running twice. However, I can't see why...

Here is my JS code:

$(document).on('pageinit', '#searchPage', function(){

alert("This alert box runs twice!");

$.ajax({
    dataType: "json",
    url: '../JS/food.json',
    success: function(result){
        var output = '';
        $.each(result, function(i, food){
            output += '<li><a href="#" id="' + i + '" class="food_info"><h2>' + food.name + '</h2><p>' + food.calories + ' Calories per 100g</p></a></li>';
        });

        $('#searchFood').html(output).promise().done(function(){
            $(this).on("click", ".food_info", function (e) {
                e.preventDefault();
                $("#foodInfo").data("result", result[this.id]);
                $.mobile.changePage("#foodInfo", {transition : "slide", reverse : false});
            });

            $('#searchFood').prepend('<li data-role="list-divider" data-theme="c" role="heading">Food and Meals:</li>');
            $(this).listview("refresh");
        });
    },

    error: function(jqXHR, textStatus, errorThrown){
            alert(errorThrown);
    }
});
});

After doing some research I think it may be related to the $.mobile.changePage but was unable to figure out any alternative. Any help would be really appreciated! Thanks in advance!

来源:https://stackoverflow.com/questions/22172255/why-is-pageinit-firing-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!