How to make document ready execute multiple times

本秂侑毒 提交于 2019-12-01 18:35:34

How about moving your document ready code to your dialog callback?

var rateDialog;
$('<div id="ratingloaderDiv"></div>')
    .load("ratingDialog.jsp", function() {
        rateDialog = $(this).dialog({
            autoOpen: true,
            minHeight:275,
            width: 400,
            height: 350,  
            open: function( event, ui ) {
                $("#showDialogMessage").hide();
                $('#reviewArea').val('');
                $('#source').attr('checked', false);
                $('#destination').attr('checked', false);

                // Document ready
                $(".rateStar").show();
                $(".rateCls").rating();
                alert('hi'); 
            }
        });
    });

Moving the dialog call to your load callback also ensures it doesn't run until your file is retrieved.

You could simply use an iFrame in your dialog like the below.

Also, you will want to keep a reference to your dialog and destroy it when it is closed, otherwise you will be adding it to your page over and over again

$(document).ready(function () {
    var rateDialog;
    function openRatingDialog() {
        rateDialog = $('<div id="ratingloaderDiv"><iframe id="ratingIframe" src="ratingDialog.html" width="200" height="200" frameborder="0" onload="parent.ratingsLoaded();"></iframe></div>')
            .dialog({
            autoOpen: true,
            minHeight: 275,
            width: 400,
            height: 350,
            open: function (event, ui) {
            },
            close: function (event, ui) {
                rateDialog.dialog('destroy').remove();
            }
        });
    }
    $(".rate").on("click", function () {
        // Display the dialog
        openRatingDialog();
    });
});
var curIndex=0;
function ratingsLoaded(){
            var name = 'rating'+curIndex
            var iframe = $('#ratingIframe').contents();
            iframe.find("#showDialogMessage").hide();
            iframe.find('#reviewArea').val('');
            iframe.find('#source').attr('checked', false);
            iframe.find('#destination').attr('checked', false);
            iframe.find('.rateCls').attr('name', name);
            curIndex++;

}

Here is a working example

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