:checked 는 체크박스나 라디오버튼 들 중에 체크가 되어 있는 요소만 찾는 선택자입니다.
원문 링크
http://api.jquery.com/checked-selector/
개요 : 체크박스나 라디오버튼 태그 중에 선택되어 있는 요소를 찾습니다.
- jQuery(':checked')
:checked 는 checkbox 나 radio buttons 들에 해당됩니다. 콤보(select) 박스는 :selected 선택자를 사용하세요.
예 제
체크박스를 클릭할 때마다 그 상태를 체크하여 만일 선택(체크)상태이면 체크된 상태인 개수를 알려줍니다.
<!DOCTYPE html>
<html>
<head>
<style>
div { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<form>
<input type="checkbox" name="newsletter" checked="checked" value="Hourly" />
<input type="checkbox" name="newsletter" value="Daily" />
<input type="checkbox" name="newsletter" value="Weekly" />
<input type="checkbox" name="newsletter" checked="checked" value="Monthly" />
<input type="checkbox" name="newsletter" value="Yearly" />
</form>
<div></div>
<script>
function countChecked() {
var n = $("input:checked").length;
$("div").text(n + (n <= 1 ? " is" : " are") + " checked!");
}
countChecked();
$(":checkbox").click(countChecked);
</script>
</body>
</html>미리보기
위 소스중에 보시면 n <= 1 ? " is" : " are" 의 구문이 있습니다. 삼항식이 안에 있습니다. 삼항식이란
"조건 ? 참일 때 : 거짓일때"
의 구문입니다. 위에서는 n 이 1보다 작거나 같을 때 " is" 를 출력하고, 1보다 클 때 " are" 를 출력합니다.
우리가 쉽게 볼 수 있는 if ~ else 로 표현하면
if(n <= 1) a = " is"; else a = " are";
여기서 눈여겨 보실건 삼항식이네요. ^^;; 체크되어 있는 걸 찾는 내용은 너무나 쉽습니다. 이전 처럼 체크박스 찾는 루틴 짜느라 고생할 필요 없겠어요 :) 무지 짜증나는 일이였는데요 ㅎㅎ;;
그럼 즐프하세요.
※ 본 예제는 http://www.jquery.com 에 있는 내용임을 밝힙니다.
'프로그래밍 > jQuery' 카테고리의 다른 글
| jQuery API 정복 - 자식 요소 선택하기 : jQuery('ancestor descendant') (2) | 2011.01.27 |
|---|---|
| jQuery API정복 - 특정 단어 포함 요소 선택하기 : jQuery(':contains(text)') (6) | 2011.01.27 |
| jQuery API 정복 - 클래스명으로 선택하기 : jQuery('.class') (7) | 2011.01.27 |
| jQuery 쉽게하기 - API 깨부시기, 선택자(Selector) : 하위 요소 선택 ("parent > child") (8) | 2011.01.27 |
| jQuery 쉽게하기 - API 깨부시기, 선택자(Selector) : 체크박스를 찾자 (6) | 2011.01.26 |
| jQuery 쉽게하기 - API 깨부시기, 선택자(Selector) : button 을 찾자 (3) | 2011.01.25 |
| jQuery 쉽게하기 - API 깨부시기, 선택자(Selector) : [name^="value"] (4) | 2011.01.24 |
| jQuery 쉽게하기 - API 깨부시기, 선택자(Selector) : [name!="value"] (5) | 2011.01.24 |