2015-01-03 3 views
0

div "containerDIV"내부에서 변경된 체크 박스를 "얻으려면 어떻게해야합니까?JQUERY ON ('change') 체크 박스 값과 상태 가져 오기

보기 :

@model MyModel 

<div id="containerDIV"> 
<ul id="CheckBoxList"> 
@foreach (XObject t in MyModel.XCollection) 
     { 
     <li> 
      <input type="checkbox" value="@t.id"/> 
     </li> 
     } 
</ul> 
자바 스크립트 (jQuery를) 측면에서

, 난이 있습니다,

$('#containerDIV').on('change', '#CheckBoxList', function (event) { 

    var id = $(this).val(); // this gives me null 
    if (id != null) { 
     //do other things 
    } 

}); 

그것은 $this 체크 박스 아니라는 것을 분명이 사업부의 containerDIV 또는 checkBoxList

어떻게 채널에 연결할 수 있습니까? eckbox의 상태와 가치?

+1

'event.target'이 작동합니다 @billyonecan 변경 이벤트 – billyonecan

+0

를 실행 한 DOM 요소가 될 것입니다,하지만 난에 노력하고있어 – Tuco

답변

5

경우 input DOM을로드 한 후 동적으로 생성되지 않는 당신은 단지 호출 할 수

$('#CheckBoxList input[type=checkbox]').change(function() { 

    var id = $(this).val(); // this gives me null 
    if (id != null) { 
    //do other things 
    } 

}); 

아니면 그냥 클릭하기 년대 input을 대상으로 할 필요가 .on()을 사용하기를 :

$('#CheckBoxList').on('change', 'input[type=checkbox]', function() { 

    var id = $(this).val(); // this gives me null 
    if (id != null) { 
    //do other things 
    } 

}); 

FIDDLE

+0

내가 할 수 없다.보기가 부분보기이고, 바뀌면 JQUERY와 DOM 사이의 "바인딩"이 느슨해진다. – Tuco

+1

@ 216 두 번째 예제를 보자. – jmore009

+0

@ 216이 추가되었다. 바이올린도 – jmore009

0

가능한 경우 이벤트를 속성으로 추가하십시오 (예 :). 10. 이 함수는 요소를 함수에 반환하므로 ID와 같은 데이터를 가져 오거나 그렇게 수정할 수 있습니다.


행운을 빌어 요! billyonecan 의견에 따라

0

이 그것을 할 수있는 또 다른 방법입니다

$('#containerDIV').on('change', '#CheckBoxList', function (event) { 

var id =$(event.target).val(); 
if (id != null) { 
    //do other things 
} 

});