【问题标题】:problems displaying Javascript message within <p>在 <p> 中显示 Javascript 消息的问题
【发布时间】:2011-02-08 01:07:20
【问题描述】:

我使用 jQuery-UI 可排序,效果很好。我遇到的问题是 “新订单已保存!”“保存失败” 消息未显示在 区域中。该函数要么没有执行,要么什么的。

下面是.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");
                            }
                        },

谁能告诉我如何在

区域显示消息?

任何帮助将不胜感激。

谢谢

【问题讨论】:

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


    【解决方案1】:

    请务必添加结束标签:

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

    更新:还要确保成功函数甚至被调用——使用像 Firebug (Firefox) 这样的工具或 Chrome 或 Safari 中的开发者工具来检查网络流量以查看结果你的电话是。

    【讨论】:

      【解决方案2】:

      你不要在 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");
      }
      

      【讨论】:

      • @jmm:这将选择页面上的所有p 元素。 OP 正在尝试将新段落附加到现有 DOM 元素。
      • 确实如此,但我没有在他的代码中看到任何其他 p 元素。
      • @jmm:在这种情况下,调用$('p') 将返回一个空的元素列表,随后的链式jQuery 调用将无效。
      • 是的,你的方法是正确的,这个问题不清楚他是想每次只更新单个 p,还是每次都继续添加新的 p 元素。
      • @andrew:我在 fire bug 控制台中仅使用单个 &lt;p&gt; 尝试了他的代码,它工作得很好。 (并不是说这是正确的做法)所以也许@Atiq 应该开始查看来自$.ajax 调用的返回数据。也许首先尝试一个简单的警报以确保成功功能实际触发,然后查看 data.d 包含的内容。
      猜你喜欢
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多