2011-02-08 7 views
3

나는 다음이의 jQuery 복제 및 이름을 입력 필드

$(function(){ 
    // Control clone 
    $('div.addControl a.button').click(function (e){ 
     e.preventDefault(); 
     var parent = $(this).closest('.section').find('.parent:last'); 
     var parentInput = parent.clone(); 
     parentInput.find("input").val(""); 
     parent.after(parentInput); 

    }); 
    $('div.validGroup a.iconClose').live('click', function (e){ 
     e.preventDefault(); 
     if ($(this).closest('.section').find('.parent').length > 1){ 
      $(this).closest('div.parent').remove(); 

     } 
    }); 
    reflesh(); 
}); 
  1. (가) 버튼을 입력 필드의 값과 클론 그룹을 제거합니다 "추가"를 클릭 (2 개의 입력 필드).
  2. 클릭하면 링크 그룹에

질문 제거 "제거"추가 또는 새 그룹을 제거 할 때, 입력 필드는 예를 들어 name="items[INDEX].first"name="items[INDEX].last"

로 이름이 변경 될 수 있도록 내가 그것을 바꿀 것입니다 방법에 대해 설명합니다. 하나의 "그룹"있을 때, 입력 필드 이름을 것이다 : 나는 다른 하나를 추가하면

  • name="items[0].first"
  • name="items[0].last"

가, 새가있는 것

  • name="items[1].first"
  • name="items[1].first"

등등. 첫 번째 파일 (items[0].first)을 제거하면 두 번째 파일의 입력 이름이 "items[1].first"에서 items[0].first으로 수정됩니다. 내가 그것을 알아 냈

답변

4

enter image description here

감사 : 여기

난 모습입니다

var size = parseInt($('.form .section .parent').size()); 
$('.form .section .parent').each(function(index){ 
    $(this).find('input.text').each(function(){ 
     $(this).attr("name", $(this).attr("name").replace($(this).attr("name").match(/\[[0-9]+\]/), "["+index+"]")); 
    }); 
    if (size > 1) { $(this).find('a.iconClose').show(); }else{ $(this).find('a.iconClose').hide(); } 
}); 
+1

) .attr ("이름 ") .match (/ \\ [[0-9] + \\] /),"[ "+ index +"] "))'replace (/ \\ [\ d + \\] /, [ "+ index +"] ")'를 직접 입력하십시오. – Felipe

0
$('.add-more').on('click',function(){ 
var newelement= $(".form-content").eq(0).clone(); 
var num = $('.form-content').length; 
var newNum = num + 1; 
newelement.find('input').each(function(i){ 
    $(this).attr('name',$(this).attr('name')+newNum); 
    $(this).attr('id',$(this).attr('id')+newNum); 
}); 
$('.form-content').last().after(newelement); 
}); 
대신 $ (이 (대체`의
+0

코드 전용 답변은 실제로 대답이지만 약간의 설명 (OP 코드가 작동하지 않는 이유, 코드 뒤에있는 일반적인 아이디어, 중요한 사항, 경고 등)로 인해 더 나은 결과를 얻을 수 있습니다. 대답. – lfurini

관련 문제