2015-01-20 3 views
2

나는이 html을 데이터베이스의 데이터를 기반으로 동적으로 생성했습니다. 사용자가 라디오 버튼을 클릭 할 때 ajax를 사용하여 웹 서비스를 호출하고 사용자가 선택할 수있는 다른 라디오 버튼 목록을 만들 수 있도록 마법사를 만들려고합니다. 초기 페이지로드는 정상적으로 작동하지만 이벤트 발생과 관련된 문제가있는 동적 라디오 버튼 목록을 만들 때 발생합니다. 이 기능을 사용하려면 문서 또는 특정 div를 다시로드해야합니까?라디오 버튼이 발사되지 않아도 변경됨

이 라디오 버튼 중 하나를 선택하면 "change"이벤트를 트랩하고 싶습니다. 아래의 jQuery는 이벤트를 캡처하지 않습니다. 내가 뭘 잘못하고 있는지에 대한 제안이 있니?

HTML :

<input name="productModel" id="Basic-jacketCustom" type="radio" value="Basic jacket"><label for="Basic-jacketCustom">Basic jacket</label> 
<input name="productModel" id="Platinum-jacketCustom" type="radio" value="Platinum jacket"><label for="Platinum-jacketCustom">Platinum jacket</label> 
<input name="productModel" id="Platinum-Pro-jacketCustom" type="radio" value="Platinum Pro jacket"><label for="Platinum-Pro-jacketCustom">Platinum Pro jacket</label> 
<input name="productModel" id="Riding-jacketCustom" type="radio" value="Riding jacket"><label for="Riding-jacketCustom">Riding jacket</label> 

jQuery를 :

$('input[name="productModel"]').change(function() { alert("Event 
Fires"); }); 
+0

작품 : 다음의 예를보십시오 http://jsfiddle.net/yjxs36pq/ 당신의 경고가 어쩌면 같은 줄에있을 필요가 있겠습니까? 동적 HTML 요소를 만들려면 .on jQuery를 사용해야합니다. http://api.jquery.com/on/ – 97ldave

답변

5

당신이 "동적으로 생성"으로 무엇을 의미합니까? 페이지를로드 한 후 입력을 DOM으로 푸시하고 있습니까? 그렇다면 Event Delegation을 사용해야합니다. 나를 위해

$('body').on('change', 'input[name="productModel"]', function() { 
    alert("Event Fires"); 
}); 
+0

그게 해결책입니다. 고마워요. 초기 페이지로드 후에 데이터를 dom으로 푸시하는 것이 문제였습니다. 귀하의 빠른 답변에 감사 드리며, 여기에있는 조언을 언제나 감사드립니다. – BriceTRockindale

+0

'input : radio [name = productModel] : checked'를 사용했습니다. 희망이 도움이됩니다. – stom

관련 문제