개요 : input 태그 중에 type 이 checkbox 인 요소를 찾습니다.
- jQuery(':checkbox')
$('checkbox') 는 $('[type=checkbox]') 로 사용할 수 있습니다. 가상 클래스(pseudo-class) 선택자(":" 와 같은)를 사용하실 때는 그 앞에 태그명이나 다른 선택자를 포함하시기를 권장합니다. 그렇지 않으면 모두 선택하기 선택자("*") 가 암묵적으로 사용하게 됩니다. 즉, $(':checkbox') 은 $('*:checkbox') 나 마찬가지입니다. 그래서 $('input:checkbox')로 앞에 input 과 같은 태그명을 같이 사용하시기를 권장합니다.
지금 예제들은 사실 API 에 대한 설명을 위한 간단한 것들이기 때문에 순수한 코드 목적으로 많이 작성되어 있습니다. 즉 실사용에 바로 사용하기에는 조금씩 부족한 내용들입니다. 본 코드를 학습 이외의 더 큰 목적으로 사용하시려면 조금 더 커스터마이징을 하셔야 할 겁니다. 이런 것에 대한 우려를 나타낸 설명이 덪붙여져 있는 것 같네요.
예 제
문서에 있는 input 태그들 중에 type 속성(attribute)의 값이 "checkbox" 인 것을 찾아 테두리를 빨간 색으로 두르고, 그 개수를 표시해 줍니다.
<!DOCTYPE html> <html> <head> <style> textarea { height:25px; } </style> <script src="http://code.jquery.com/jquery-1.4.4.js"></script> </head> <body> <form> <input type="button" value="Input Button"/> <input type="checkbox" /> <input type="checkbox" /> <input type="file" /> <input type="hidden" /> <input type="image" /> <input type="password" /> <input type="radio" /> <input type="reset" /> <input type="submit" /> <input type="text" /> <select><option>Option<option/></select> <textarea></textarea> <button>Button</button> </form> <div> </div> <script> var input = $("form input:checkbox").wrap('<span></span>').parent().css({background:"yellow", border:"3px red solid"}); $("div").text("For this type jQuery found " + input.length + ".").css("color", "red"); $("form").submit(function () { return false; }); // so it won't submit </script> </body> </html>
미리보기
출처 : http://findfun.tistory.com/85