在<p> </p>中显示Javascript消息的问题

时间:2011-02-08 01:07:20

标签: javascript jquery asp.net web-services jquery-ui

我使用jQuery-UI sortable工作正常。我遇到的问题是“新订单已保存!”“保存失败”消息未显示在&lt; p>区域。该函数要么没有执行,要么就是其他东西。

以下是.aspx页面的代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="jQuery/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script src="jQuery/jquery.min.js" type="text/javascript"></script>
    <script src="jQuery/jquery-ui.min.js" type="text/javascript"></script>
    <script src="jQuery/json2.js" type="text/javascript"></script>
    <script src="jQuery/jquery-ui-i18n.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $("#sortable").sortable({ placeholder: "vacant" });
            $("#sortable").disableSelection();
            $("#sortable input[type=text]").width($("#sortable img").width() - 10);
            $("#sortable label").mouseover(function () {
                $(this).parent().children("input[type=text]").show().val($(this).html());
                $(this).hide();
            });
            $("#sortable input[type=text]").mouseout(function () {
                $(this).parent().children("label").show().html($(this).val());
                $(this).hide();
            });
            $(".ContainerDiv").hover(
                function () {
                    $(this).find(".deleteClass").show();
                },
                function () {
                    $(this).find(".deleteClass").hide();
                });
            $(".deleteClass").click(function () {
                $(this).closest("li").remove();
            });
            $("#orderPhoto").click(function () {
                var photos = $.map($("li.ui-state-default"), function (item, index) {
                    var imgDetail = new Object();
                    imgDetail.Id = $(item).find("img").attr("id");
                    imgDetail.Caption = $(item).find("label").html();
                    imgDetail.Order = index + 1;
                    return imgDetail;
                });
                var jsonPhotos = JSON.stringify(photos);
                $.ajax({
                    type: "POST",
                    contentType: "application/json",
                    data: "{photos:" + jsonPhotos + "}",
                    url: "WebService.asmx/updatePhoto",
                    dataType: "json",
                    success: function (data) {
                        if (data.d === "saved") {
                            $("<p>").text("New order saved!")
                .addClass("success").appendTo("#left");
                        } else {
                            $("<p>").text("Save failed")
                .addClass("failure").appendTo("#left");
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        debugger;
                    }
                });
            });
        });
    </script>
    <style type="text/css">
        #sortable
        {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }
        #sortable li
        {
            position: relative;
            margin: 3px 3px 3px 0;
            padding: 1px;
            float: left;
            text-align: left;
        }
        #sortable .vacant
        {
            border: 3px dotted #66d164;
            width: 150px;
            height: 175px;
            background-color: #fff;
        }
        #outerWrap
        {
            width: 1004px;
            margin: auto;
            position: relative;
            background-color: #eee;
            border: 1px solid #999;
        }
        #outerWrap:after
        {
            content: ".";
            display: block;
            visibility: hidden;
            clear: both;
        }
        #left
        {
            width: 218px;
            float: left;
        }
        #images
        {
            margin: 0;
            padding: 0;
            float: left;
            width: 786px;
        }
        h1
        {
            font: italic normal 24px Georgia, Serif;
            text-align: center;
            margin: 10px 0;
        }
        p
        {
            margin: 0;
            font: 12px Arial, Sans-serif;
            padding: 0 10px;
        }
        .deleteClass
        {
            /* PhotoListItem  is relative so relative to it */
            position: absolute;
            top: 1px;
            right: 3px;
            background: black;
            color: Red;
            font-weight: bold;
            font-size: 12px;
            padding: 5px;
            opacity: 0.60;
            filter: alpha(opacity="60");
            margin-top: 3px;
            display: none;
            cursor: pointer;
        }
        .deleteClass:hover
        {
            opacity: 0.90;
            filter: alpha(opacity="90");
        }
        .image_resize
        {
            width: 150px;
            height: 150px;
            border: 0;
        }
        .success, .failure
        {
            margin: 0 0 0 10px;
            padding: 4px 0 4px 26px;
            position: absolute;
            bottom: 18px;
            font-weight: bold;
        }
        .success
        {
            background: url('../img/tick.png') no-repeat 0 1px;
            color: #12751c;
        }
        .failure
        {
            background: url('../img/cross.png') no-repeat 0 0;
            color: #861b16;
        }
    </style>
</head>
<body>
    <form id="form2" runat="server">
    <div id="outerWrap">
        <div id="left">
            <h1>
                Image Organiser</h1>
            <p>
                Re-order the images by dragging an image to a new location. Your changes will be
                saved automatically.</p>
        </div>
        <div id="images">
            <asp:ListView ID="ListViewAlbumPhotoView" runat="server" GroupItemCount="15">
                <LayoutTemplate>
                    <ul id="sortable">
                        <li id="groupPlaceholder" runat="server">1</li>
                    </ul>
                </LayoutTemplate>
                <GroupTemplate>
                    <tr id="itemPlaceholderContainer" runat="server">
                        <td id="itemPlaceholder" runat="server">
                        </td>
                    </tr>
                </GroupTemplate>
                <ItemTemplate>
                    <li class="ui-state-default">
                        <div class="ContainerDiv">
                            <div class="deleteClass">
                                X</div>
                            <img id='<%#Eval("photo_id")%>' src='<%# "uploads/" + Eval("photo_file_name")%>'
                                alt="" class="image_resize" />
                            <div style="height: 25px; margin-top: 3px">
                                <label>
                                    <%# Eval("photo_title")%></label>
                                <input type="text" style="display: none" />
                            </div>
                        </div>
                    </li>
                </ItemTemplate>
            </asp:ListView>
            <input type="button" id="orderPhoto" value="Save change" />
        </div>
    </div>
    </form>
</body>
</html>

问题在于

success: function (data) {
                            if (data.d === "saved") {
                                $("<p>").text("New order saved!")
                    .addClass("success").appendTo("#left");
                            } else {
                                $("<p>").text("Save failed")
                    .addClass("failure").appendTo("#left");
                            }
                        },

有人可以告诉我如何在&lt;中显示消息。 p>区域?

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:2)

请务必同时添加结束标记:

$("<p></p>").text("Save failed").addClass("failure").appendTo("#left");

更新:确保甚至调用成功功能 - 使用Chrome或Safari中的Firebug(Firefox)或开发人员工具等工具检查网络流量,了解具体功能你的电话结果是。

答案 1 :(得分:1)

你不要在jquery函数

里面的p周围使用括号

尝试$('p')

您是否正在尝试更新已存在的单个段落元素?或者你想插入 每次都有一个新的段落元素。如果你想要第二种情况,只需一次组合所有内容。

if (data.d === "saved") {
    $("<p class='success'>New order saved!</p>").appendTo("#left");
}
else {
    $("<p class='failure'>Save failed!</p>").appendTo("#left");
}
相关问题