2010-01-08 60 views
3

这应该是我的jQuery的可排序的最后一个问题......一会儿:)动态元素添加到jQuery的可排序

我有我能够从动态删除元素的列表。当用户点击该元素的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插件来验证输入)。

我认为最大的挑战是动态创建一个可以附加到列表中的对象。任何人都可以为我指出正确的方向吗?

回答

2

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> 

以上是我用做一样的,你要寻找的。

+0

这是完美的,我想象的痛苦少得多。谢谢! – splatto 2010-01-08 06:09:24