2010-11-19 4 views
0

꽤 간단한 문제라고 생각합니다.jQuery Selector

큰 테이블이 있습니다. 각 행에는 두 개의 확인란이 있습니다. 하나가 선택되어 있다면 같은 행에있는 다른 체크 박스의 속성을 체크하기를 원합니다.

<script language="javascript" type="text/javascript"> 
$(document).ready(function(){ 
$(".BookTable input[name=attended]").click(function() { 

    if ($(this).attr("checked") == true) 
    { 
     $("input").parent().parent("input[name=attended]").attr("checked","yes"); 
    } 
});  
}); 
</script> 

<table class="BookTable adj-table"> 
<tr> 
<td>Joe</td> 
<td>Bloggs</td> 
<td><input name="booking" id="booking" type="checkbox" value="1" /></td> 
<td><input name="attended" id="attended" type="checkbox" value="1" /></td> 
</tr> 
<tr> 
<td>Dave</td> 
<td>Smith</td> 
<td><input name="booking" id="booking" type="checkbox" value="1" /></td> 
<td><input name="attended" id="attended" type="checkbox" value="1" /></td> 
</tr> 
</table> 

My Code on Paste Bin

지금까지 나는 단지 모든 체크 박스를 선택을 얻을 수가 있습니다.

+0

질문에 항상 실제로 * 포함 *하십시오. 링크는 괜찮지 만 StackOverflow의 질문과 답변은 독립 실행 형이어야합니다. 사용자는 질문을 읽으려는 다른 곳을 클릭해서는 안됩니다. –

+0

'id' 속성은 문서에서 고유해야합니다. 이 예제에서는 사용되지 않았으므로이 둘을 모두 삭제할 수 있습니다. – nickf

+2

'id' 속성 **은 고유해야합니다 **. 그것에 대해 "해야"하지 않습니다. :-) –

답변

0
$(".BookTable :checkbox").click(function(){ 
    if ($(this).is(":checked")) 
     $(this).parents("tr").find(":checkbox").attr("checked", "checked"); 
    else 
     $(this).parents("tr").find(":checkbox").removeAttr("checked"); 
}); 
+0

'.attr ("checked", bool)'은 * 모든 브라우저에서 작동하며, HTML 마크 업과 DOM을 혼동하고 있습니다. 별개로 다른 것들입니다. –

+0

감사합니다.이 모든 것들이 테스트되었을 때 잘 작동하는 것 같습니다. 이 옵션은 선택을 취소 할 때 두 상자의 선택을 취소하지 않기 때문에 내 필요에 가장 적합합니다. – Sam

3

이 같은 내부 <tr> 다음 .find() 입력까지 이동 .closest()를 사용할 수 있습니다

$(".BookTable :checkbox").change(function() { 
    $(this).closest("tr").find(":checkbox").attr("checked", this.checked); 
}); 

You can test it out here합니다. 당신은 같은 이름을 가진 두 개의 아이디의를 가질 수 없습니다

$(".BookTable :checkbox").change(function() { 
    if(this.checked) 
    $(this).closest("tr").find(":checkbox").attr("checked", true); 
}); 
+0

'checked' 속성을 어떤 브라우저에서 검사하도록 설정하지 않았습니까? – hunter

+0

@hunter - 아니요, 부울 속성입니다. –

+0

IE에서'checked = "false"'여전히 체크되었습니다 – hunter

0

당신이 그들을 클래스

해야한다 : 당신은 확인하지만 다른 선택을 취소 확인하고 싶은 경우에, 다만 if() like this 거기에 추가