2013-10-24 107 views
0

我正在創建一個動態表單,我希望有機會將選定的克隆表刪除給用戶。每個表格都有一個帶有唯一ID的複選框。這裏是我的代碼:Jquery /如何刪除複選框選擇的克隆表?

<button class="addOffer">Add Offer</button> 
    <button class="delOffer">Delete Offer</button> 
     <div id=offer> 
     <TABLE id="AC" width="400px" border="0"> 
       <TR> 
        <TD class="g1"> 
        <INPUT type="checkbox" name="chk"/> 
        </TD> 
        <TD class="g1"><SELECT name="ACREG"> 
        <OPTION VALUE=""></OPTION> 
        <OPTION VALUE="ACA">ACA</OPTION> 
       <OPTION VALUE="ACB">ACB</OPTION> 
       </SELECT></TD> 
        <TD class="g1"></TD> 
        <TD align=right class="g1"><INPUT type="button" value="Add Pic" onClick="AddPic" /></TD> 
       </TR> 
     </TABLE> 
     </div> 

<script type="text/javascript"> 
    var i = 1; 
    $('.addOffer').click(function() { 
     var copy = $('#AC').clone(); 
     var formId = 'AC' + i; 
     copy.attr('id', formId); 

     $('#offer').append(copy); 
     i++; 
    }); 
</script> 

我有點失去了上點擊刪除要約時所選擇的複選框如何發送到功能他的身份證的部分。

您的幫助和建議真的很感謝。 問候, POY

回答

0

,你可以添加一個類的複選框,並在delOffer單擊刪除那些最接近表檢查
HTML

<button class="addOffer">Add Offer</button> 
<button class="delOffer">Delete Offer</button> 
    <div id=offer> 
    <TABLE id="AC" width="400px" border="0"> 
      <TR> 
       <TD class="g1"> 
       <!--add the class to checkbox--> 
       <INPUT type="checkbox" name="chk" class="delCheck"/> 
       </TD> 
       <TD class="g1"><SELECT name="ACREG"> 
       <OPTION VALUE=""></OPTION> 
       <OPTION VALUE="ACA">ACA</OPTION> 
      <OPTION VALUE="ACB">ACB</OPTION> 
      </SELECT></TD> 
       <TD class="g1"></TD> 
       <TD align=right class="g1"><INPUT type="button" value="Add Pic" onClick="AddPic" /></TD> 
      </TR> 
    </TABLE> 
    </div> 

JS

$('.delOffer').click(function() { 
    $(".delCheck:checked").closest("table").remove(); 
});  

OR

$('.delOffer').click(function() { 
    $(".delCheck").each(function(i,n){ 
     //you can get the id of the table like $(n).closest("table").attr("id"); 
     if($(n).get(0).checked==true){//you can add more conditions to delete offer 
       $(n).closest("table").remove(); 
     } 
    }); 
});  

http://jsfiddle.net/bVBcJ/

+0

just perfect !!!對不起,我讀得太快了;)非常感謝! – poypoy