克隆并保留绑定事件并更改克隆元素中的属性

时间:2011-08-25 19:49:05

标签: jquery

我试图在jquery中使用.clone()。我有一个$ .post重新调整我想在克隆元素中更改的id号。如果我克隆父母和孩子来更改id属性,我该如何使用.data?我还有一个更新绑定到克隆后我想要保留的选择标记。

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);
    })
})

1 个答案:

答案 0 :(得分: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');
});
使用attr()访问

id,或者在元素的数据对象中保存id?然后你会使用数据('id','something');