【问题标题】:Dynamically Add Elements to jQuery Sortable将元素动态添加到 jQuery 可排序
【发布时间】:2010-01-08 00:43:35
【问题描述】:

这应该是我在 Jquery Sortable 上的最后一个问题......暂时:)

我有一个可以动态删除元素的列表。当用户单击元素上的 X 关闭框时,我会获取父元素(元素本身)并将其删除:

    function DeleteLink() {
        var jItem = $(this).parent();

        var LinkId = jItem[0].childNodes[1].innerText || jItem[0].childNodes[3].textContent;
        var LinkTitle = jItem[0].childNodes[2].innerText || jItem[0].childNodes[5].textContent;

        if (!confirm(String.format("Are you sure you want to delete link #{0}?\n\nTitle: {1}", LinkId, LinkTitle)))
            return;

        jItem.remove();
        $.post("/Home/DeleteLink/" + LinkId);
        showStatus("Link deleted...", 5000);
    }

如果你有兴趣,无序列表是这样创建的:

<ul id="sortable1" class="connectedSortable">
        <% foreach (Link l in Model)
             {
                 if (l.Visible == true)
                 {%>
                    <li class="photolistitem" style="min-height:50px;">
                    <div class="imagecontainer"><img src="/Content/images/link.jpg" style="float:left; padding-right:5px;" class="thumbnailimage" alt="" /></div>
                    <div id='<%=l.Id%>Id'><%=l.Id%></div>
                    <div id='<%=l.Id%>Description'><%=l.Title%></div>
                    <div id='<%=l.Id%>Description'><%=l.Description%></div>
                    <div id='<%=l.Id%>Description'><%=l.Url%></div>
                    <div class='deletethumbnail'>X</div>
                    </li>
               <%}%>
       <%}%>  
    </ul>

我想要做的是在页面底部有一个表单,以便用户可以动态添加一个元素 - 他们只需要插入一个描述、一个标题和一个 url(我将使用另一个 jquery插件来验证输入)。

我认为最大的挑战将是动态创建一个可以附加到列表的对象。谁能指出我正确的方向?

【问题讨论】:

    标签: c# javascript jquery asp.net-mvc jquery-ui-sortable


    【解决方案1】:

    jQuery

    thisDependant = $('.DependantTemplate').clone();
    $(thisDependant).removeClass("DependantTemplate");
    $(thisDependant).addClass("Dependant" + dependantNum);
    $('.DependantList').append(thisDependant);
    

    HTML

    <div class="DependantTemplate hidden">
      <div style="float:left;" class="DependantNumber spacerRight10"></div>
      <div style="float:left;" class="DependantFirstName spacerRight10"></div>
      <div style="float:left;" class="DependantLastName spacerRight10"></div>
      <div style="float:left;" class="DependantDateOfBirth spacerRight10"></div>
      <div style="float:left;" class="DependantGender spacerRight10"></div>
      <div style="float:left;" class="DependantType"></div>
      <div class="clearFloats"></div>
    </div>
    
    <div class="DependantList"></div>
    

    上面是我用来做你正在寻找的同样的事情。

    【讨论】:

    • 这很完美,而且没有我想象的那么痛苦。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2021-06-12
    • 2011-07-05
    • 2021-12-13
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    相关资源
    最近更新 更多