2014-04-09 37 views
-2

我在JQuery中爲標記創建了一個腳本,腳本目標是當您單擊空間時,它會自動創建一個新標記。JQuery - 我遇到一些問題,使用克隆()

但問題是,我想創建一個按鈕來克隆我的表單,當我點擊這個按鈕時,它克隆我的表單,但標籤只適用於最後一種形式,當您嘗試編輯標籤來自第一個創建的表單,你不能,因爲scritpt無法識別。

我創建了一個的jsfiddle解釋發生了什麼

http://jsfiddle.net/8FDjy/

我使用此代碼克隆形式,我只使用類

$(function(clone){ 
var template = $('.job .offer:first').clone(true); 
    var offersCount = 1; 
    window.addoffer = function(clone){ 
     offersCount++; 
     var offer = template.clone(true).find(':input').each(function(){ 
      var newId = this.id.substring(0, this.id.length-1) + offersCount; 
      this.name = this.id = newId; // update id and name (assume the same) 
     }).end() 
     .attr('id', 'att' + offersCount) 
     .prependTo('.job'); 
    } 
    $('.add').click(addoffer); 
}); 
+2

僅供參考,'活()'已經從jq1.9刪除+(指你的jsfiddle) –

+0

請,憑有效HTML標記改善你的jsfiddle。這些'.close'按鈕在哪裏? –

+1

爲什麼你要在函數中使用克隆參數? –

回答

0

現場已被棄用,在jquery 1.8+中刪除,使用on委託事件動態添加元素。

試試這個

$('.job').on('keypress', '.txtlinks', function (e) { //Check if space was clicked, and create a new tag 
if (e.which == 32) { 
    var tl = $('.txtlinks').val(); 
    if (tl) { 
     $(this).val('').parent().before('<li class="links"><span><input type="hidden" value="' + tl + '" name="links[]" />' + tl + '</span><a style="cursor:pointer;" class="close">[x]</a></li>'); 


     } 
    } 
}); 


$('.job').on('click', '.close', function() { 
    $(this).parent().remove(); 
}); 


$(function (clone) { 
    var template = $('.job .offer:first').clone(true); 
    var offersCount = 1; 
    window.addoffer = function (clone) { 
    offersCount++; 
    var offer = template.clone(true).find(':input').each(function() { 
     var newId = this.id.substring(0, this.id.length - 1) + offersCount; 
     this.name = this.id = newId; // update id and name (assume the same) 
    }).end() 
     .attr('id', 'att' + offersCount) 
     .prependTo('.job'); 
    } 
    $('.add').click(addoffer); 
}); 

fiddle here