2012-06-27 68 views
0

我有這個jquery達我用小提琴(與它一起工作與我的網站進行測試)。 ,我有jQuery的克隆,表單值和清空

$(document).ready(function() { 
    var regex = /^(.*)(\d)+$/i; 
    var cloneIndex = $(".clonedInput").length; 

    $("button.clone").click(function(e){ 
     $(this).parents(".clonedInput").clone() 
      .insertBefore(copy) 
      .attr("id", "clonedInput" + cloneIndex) 
      .find("*").each(function() { 
       var id = this.id || ""; 
       var match = id.match(regex) || []; 
       if (match.length == 3) { 
        this.id = match[1] + (cloneIndex); 
       } 
     }); 
     cloneIndex++; 
     return false; 
    }); 

    $("button.remove").click(function(){ 
     $(this).children(".clonedInput").remove(); 
    }); 
}); 

兩個問題,這可能是由一個問題引起的是,當我刪除了「克隆」的領域之一,它會刪除所有其他領域的除了父母,當我克隆領域,它只能起作用,並保持原始值...

任何幫助,將不勝感激!

+0

你可以給我們使用的HTML,或更好的,它的http://jsfiddle.net?我很確定這是關於'click'事件,它應該是'on'或者至少'live'。擁有HTML可以幫助我知道我是否正確。 – Calvein

+0

當然,甚至沒有想到這一點。 http://jsfiddle.net/bendrumin/N3FGW/ – bs55426

回答

0

正如我在評論中所說的,我使用on方法來實現你想要做的事情。
click方法是bind方法的別名。 bind方法不是動態,您將一個事件綁定到一個節點,這就是全部。 livedelegate方法是動態的,它們不只是將事件綁定到節點,而是檢查事件和節點是否匹配(我會解釋)。
最後,on方法在這裏協調這3種方法。以下是我所做的:

// On all the <form>s 
$('form') 
.on(
    // When we click inside the <forms> 
    'click', 
    // If the clicked element match the selector, so if it has the clone class 
    '.clone', 
    // We do stuff 
    function(){ 
     // Do stuff 
    }); 

The fiddle

+0

非常感謝! – bs55426

+0

不要忘記「接受」我的答案,如果它幫助你:) – Calvein