2011-08-25 87 views
0

我正试图在jQuery中使用.clone()。我有一个$ .post retuning一个id号码,我想改变克隆的元素。如果我克隆父母和孩子更改id属性,我该如何使用.data?我还有一个绑定到我想在克隆后保留的select标签的更新。克隆并保留绑定的事件和更改克隆元素中的属性

HTML:

<div id="basicinfobox"> 
    <table id="ethnicitytable"> 
    <tbody> 
     <tr valign="top"> 
     <td> 
      <img id="Ethnicity" ref="6" rel="ActorEthnicity" src="plussm.png"> 
     </td> 
     <td id="ethnicity22"> 
      <select id="22" rel="ActorEthnicity" name="Ethnicity"> 
      <option value="">Ethnicity</option> 
      <option value="Caribbean">Caribbean</option> 
      </select> 
      <img id="22" border="0" width="16" height="16" onclick="ethnicitydelete(this.id)" alt="Delete Ethnicity" src="deletessm.png"> 
      </td> 
    </tr> 
    </tbody> 
    </table> 
</div> 

的Jquery:

$('#basicinfobox img[src*="plus"]').click(function(){ 
    var rel=$(this).attr('rel'); 
    alert(rel) 
    var ref=$(this).attr('ref'); 
    alert(ref); 
    //$('#ethnicitytable tr td:last').clone().appendTo('#ethnicitytable tr:last'); 
    $.post('add.php', {rel: rel}, function(id){ 
     var elem = $('#ethnicitytable tr td:last')[0]; 
       clone = elem.clone(true); 
     jQuery.data(clone, "id", id); 
     clone.appendTo('#ethnicitytable tr:last'); 
     //$('#ethnicitytable td:last').attr('id', 'ethnicity'+id); 
     //$('#ethnicitytable select:last').attr('id', id); 
     //$('#ethnicitytable img[src="deletessm.png"]:last').attr('id', id); 
    }) 
}) 

回答

0
$.post('add.php', {rel: rel}, function(id){ 
    var elem = $('#ethnicitytable tr td:last')[0]; 
    clone = elem.clone(true).attr('id', 'yourNewId').appendTo('#ethnicitytable tr:last'); 
}); 

ID被使用attr()访问或者你保存在元数据对象的ID?那么你会使用数据('id','something');