2010-12-15 4 views
1

인데 여러 개의 확인란이있는 페이지가 있습니다. 확인란을 선택했는지 여부를 결정하는 ckeckbox를 클릭 할 때 호출 될 함수를 작성하고 싶습니다.체크 박스 객체의 출력 값이

<input type="checkbox" onclick="toggleVis('id', this);"/> ID 
<input type="checkbox" onclick="toggleVis('edit', this);" checked="checked"/> Edit 
<input type="checkbox" onclick="toggleVis('last', this);"/> Last 

일부 체크 박스가 선택되어 있습니다.

전달 된 참조를 기반으로이를 수행하는 방법이 있어야한다는 것을 알았 기 때문에이 값을 매개 변수로 전달했습니다.

function toggleVis(name, checkbox) 
{ 
    //if(checkbox.checked()) 
     console.log('checked'); 

     if($('.'+name).css('display') != "none") 
      $('.'+name).css('display', 'none'); 
     else 
      $('.'+name).css('display', 'table-cell'); 
} 

jQuery를 사용하고 있습니다.

+0

코드가 작동 했습니까? 무슨 일이야? Firebug를 가져와 실제로 전달 된 객체가 무엇인지 확인하십시오. – OrangeDog

답변

1

가까운 사이.

if(checkbox.checked) { 
    console.log('checked'); 
    //... 
} 

는 더 checked() 방법도 없다, 그러나 checked 특성이있다. 코드는 클릭 수에서만 작동 할 수 있습니다. 아마 onchange가 좋을까요?

1

(함수처럼 호출하는 대신) checkboxobj.checked 속성을 살펴보십시오. 귀하의 경우에는 if (checkbox.checked) { ... }을 참조하십시오. 일부 JQ의 선택에 의해 먼저 체크 박스를 찾으려면

var checked = $(checkbox).is(':checked'); 

도움 : 자세한 내용은

0
<input type="checkbox" class="checkthis"> ID 
<input type="checkbox" class="checkthis" checked="checked"> Edit 
<input type="checkbox" class="checkthis"> Last 

$(input.checkthis).click(function() { 
    if($(this).is(':checked') { 
    // do checked stuuf 
    } else { 
    // do not checked stuff 
    } 
}); 
1

jQuery로 이것을 수행하려면 다음을 시도해보십시오. HTML 요소에 'onclick'또는 'onchange'를 직접 포함하는 대신 확인란에 바인딩한다는 점에 유의하십시오.

$('[type=checkbox]').click(function(){ 
    if($(this).attr('checked')){ 
    console.log('checked'); 
    if($('.'+$(this).attr('name')).css('display') != "none") { 
     $('.'+$(this).attr('name')).css('display', 'none'); 
    } else { 
     $('.'+$(this).attr('name')).css('display', 'table-cell'); 
    } 
    } 
});