0

我有这个代码使用php代码动态生成: -

<div class="mailList" id="M_6">
    <div class="mailListHeader" id="H_6">
        <img style="float:right; display:none;" class="loaderIMG" id="LOADER_6" src="images/preloader.gif">
        Sent by <strong>Admin</strong> on <strong>Oct 03 2013 02:53 PM</strong> to <strong>Received Response</strong> for Quarter <strong>3</strong> Year <strong>2013</strong>.<br>
        Subject: <strong>Test Mail</strong><br>
    </div>

    <div class="mailListContent" id="C_6">
        <div class="closeContent" id="CC_6">Close [x]</div>
        <span id="SPAN_6"></span>
    </div>

    <div class="mailListFooter" id="F_6">
        <span class="mailContentBtn" id="MCBTN_6" style="font-size:11px; color:#09C; cursor:pointer;">
            View Content
        </span>
        <span class="mailListBtn" id="MLBTN_6" style="float:right; font-size:11px; color:#C06; cursor:pointer;">
            Successfull-[0] Failed-[4]
        </span>
    </div>
</div>

然后,用户可以单击查看内容成功-[0] 失败-[4]将发出 ajax 请求,而不是在 div mailListContent 中显示结果。下面是 jquery ajax 请求的代码:-

$(".mailContentBtn, .mailListBtn").click(function(){
    var currentId = $(this).attr('id');
    currentId = currentId.split("_");
    var actualId = currentId[1];

    if($("#C_"+actualId).is(":visible")) {
        $("#C_"+actualId).hide("slow","swing");
    }
    $("img#LOADER_"+actualId).show();

    if(currentId[0]=="MCBTN") {
        var dataString ="action=getMailContentByID&mailID="+actualId;  
    } else {
        var dataString ="action=getMailListByID&mailID="+actualId;
    }

    $.ajax({
        type: "POST",
        url: "include/getMail.php",
        data: dataString,
        cache: false,
        async: false,
        success: function(html) { 
            $("#SPAN_"+actualId).empty();
            $("#SPAN_"+actualId).append(html);

            $("#C_"+actualId).show("slow","swing");
            $("img#LOADER_"+actualId).hide();
        } 
    });
});

请求和事件工作正常,问题是每次用户点击查看内容成功-[0] 失败-[4]加载图像不显示。正如你所看到的,我为每个加载图像提供了一个唯一的 ID,而不是只有 1 个加载图像会显示在 clik 上。在谷歌浏览器中检查代码没有错误。我该如何解决这个问题?

谢谢你。

4

2 回答 2

3

In your call to $.ajax, change the "async" option to "true". Because in your case, the $.ajax is blocking the ui thread in displaying the loading image as it is executed synchronously.

于 2013-10-24T05:20:32.323 回答
0

你错过了:

$(document).ready(function () {
});

尝试这个:

<script>
        $(document).ready(function () {
            $(".mailContentBtn, .mailListBtn").click(function () {
                var currentId = $(this).attr('id');
                currentId = currentId.split("_");
                var actualId = currentId[1];

                if ($("#C_" + actualId).is(":visible"))
                    $("#C_" + actualId).hide("slow", "swing");

                $("img#LOADER_" + actualId).show();

                if (currentId[0] == "MCBTN") {
                    var dataString = "action=getMailContentByID" +
                            "&mailID=" + actualId;
                }
                else {
                    var dataString = "action=getMailListByID" +
                            "&mailID=" + actualId;
                }

                $.ajax({
                    type: "POST",
                    url: "include/getMail.php",
                    data: dataString,
                    cache: false,
                    async: false,
                    success: function (html) {
                        $("#SPAN_" + actualId).empty();
                        $("#SPAN_" + actualId).append(html);

                        $("#C_" + actualId).show("slow", "swing");
                        $("img#LOADER_" + actualId).hide();
                    }
                });
            });
        })


    </script>
于 2013-10-24T05:22:42.517 回答