Limit the number of available checkboxes in javascript / 자바스크립트로 체크할 수 있는 항목 갯수 제한하기.

<form>
You can choose 2 in maximum :
<input type=checkbox name=box1 id="box1" onClick="countChoices(this)"><label for=box1>Frozen</label>
<input type=checkbox name=box2 id="box2" onClick="countChoices(this)"><label for=box2>Finding Nemo</label>
<input type=checkbox name=box3 id="box3" onClick="countChoices(this)"><label for=box3>the Beauty and the Beast </label>
</form>
<input type="button" value=" Close " onClick="self.close( )">

<script>
function countChoices(obj) {
var max = 2; // 최대로 선택할 수 있는 박스 수
var box1 = obj.form.box1.checked;  // 사용할 체크박스
var box2 = obj.form.box2.checked;
var box3 = obj.form.box3.checked;
var count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0);
if (count > max) {
alert("최대 " + max + " 개 까지만 선택할 수 있습니다");
obj.checked = false;
   }
}
</script>

------------------------------------------------------------------------------------------


You can choose 2 in maximum :

댓글

이 블로그의 인기 게시물

Lottery in javascript / 자바스크립트로 경품추첨기 만들기

Accordion Menu in jQuery / 제이쿼리로 아코디언 메뉴 만들기

Quick Menu(flying menu) in jQuery / 제이쿼리로 퀵메뉴 바 만들기