2010-06-07 6 views
1

다음 코드를 사용하여 일부 동적 체크 박스를 생성했습니다. 이것은 처음으로 작동하고 체크 박스 chk2를 페이지에 추가 한 다음 $ ('# newLink')에 대한 트리거가 발생한 후 클릭이 작동하지 않습니다. 이걸 도와주세요.JQuery 체크 박스 생성

<div id="chkBoxesDiv"> 
    <input type="checkbox" id="chk1" ></input> 
    <input id="answerText" type="text" size="30" ></input>&nbsp; 
    <input type="button" value="add new" id="newLink"/> 
</div> 

$(document).ready(function(){ 

$('#newLink').click(function (event){ 
    var i = 0; 
    //To count the children 
    $("#chkBoxesDiv").children().each(function(){ 
     var child = $(this); 
     if(child.is(":checkbox")){ 
      i++; 
     } 
    }); 

    //prevent action 
    event.preventDefault(); 

    //get textbox value to fill checkbox 
    var text = $("#answerText").val(); 
    alert(i); 

    //if text not empty do stuff 
    if(text != ""){ 
    //add label 
     $("#chk"+i).after("<label for=\"chk"+i+"\" id=\"lblchk"+i+"\">"+text+"</label>"); 


     $("#newLink").remove(); 
     $("#answerText").remove(); 
     $("#lblchk1").after("<br /><input type=\"checkbox\" id=\"chk"+(1+i)+"\" ></input><input type=\"text\" id=\"answerText\" size=\"30\" ></input>&nbsp;<input type=\"button\" value=\"add new\" id=\"newLink\"/>"); 
    } 
    }); 
}); 
+1

* 관련 * http://stackoverflow.com/questions/1359018/in-jquery-how-to-attach-events-to-dynamic-html-elements –

답변

2

.live jquery 명령을 사용하십시오.

페이지에 요소를 추가 할 때 자동으로 클릭 이벤트가 발생하지 않습니다. 그들에게 할당해야합니다

+0

형님, 고마워요! – Hash