Javascript [ 거래처1 ]

양혜정·2024년 4월 13일
0

HTML/CSS/JS 실행

목록 보기
54/60


HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>체크박스에 대해서 알아봅니다. -1</title>

<link rel="stylesheet" href="css/01.css">
<script type="text/javascript" src="js/01.js"></script>

</head>
<body>

	<div align="center">
		<h2 class="h2class" id="underline">거래처등록-1</h2>
		<table>
			<tr>
				<td class="title">거래처명</td>
				<td class="data"><input type="text" size="20" /></td>
			</tr>
			<tr>
				<td class="title">주소</td>
				<td class="data"><input type="text" size="50" /></td>
			</tr>
			<tr>
				<td class="title">전화번호</td>
				<td class="data">
				    <input type="text" size="5" maxlength="3" />&nbsp;-&nbsp;    
				    <input type="text" size="5" maxlength="4" />&nbsp;-&nbsp;
				    <input type="text" size="5" maxlength="4" />
				</td>
			</tr>
			<tr>
				<td class="title">품명(국산)</td>
				<td class="data">
					<input type="checkbox" name="product_kor" id="product_kor1" value="kor01" /><label for="product_kor1">메인보드</label>&nbsp;
					<input type="checkbox" name="product_kor" id="product_kor2" value="kor02" /><label for="product_kor2">모니터</label>&nbsp;
					<input type="checkbox" name="product_kor" id="product_kor3" value="kor03" /><label for="product_kor3">프린터</label>&nbsp;
					<input type="checkbox" name="product_kor" id="product_kor4" value="kor04" /><label for="product_kor4">하드디스크</label>&nbsp;
					<input type="checkbox" name="product_kor" id="product_kor5" value="kor05" /><label for="product_kor5">메모리</label>&nbsp;
					<input type="checkbox" name="product_kor" id="product_kor6" value="kor06" /><label for="product_kor6">CPU</label>&nbsp;
				</td>
			</tr>
			
			<!-- 
			   >>> === checkbox 제어의 기본개념 === <<<
			    - checkbox 는 동일한 이름을 가진 체크박스들이 여러개 존재하며
			      다수의 선택을 가능하게 하는 기능을 제공한다.
				  checkbox 는 배열의 개념이 필요하기 때문에 getElementById 가 아닌
				  getElementsByName 을 이용하여 제어한다.
				  그래서 id 가 아닌 name 을 이용하기 때문에 태그에 id 를 지정할 필요가 없다.
				  반드시 태그에  name 을 지정해야만 한다.  
				              
				>>> === checkbox 의 주요속성 === <<<
					1. name    => 체크박스의 이름.
					2. length  => 동일한 이름의 체크박스의 갯수.
					3. checked => 체크박스의 체크여부. 체크가 되어지면 true, 체크가 해제되면 false 를 리턴함.
					4. value   => 체크박스의 값.             
			-->
			
			<tr>
				<td class="title">품명(중고품)</td>
				<td class="data">
				    <input type="checkbox" name="product_old" id="product_old1" value="old01" onclick="onlyOneCheck(this)" /><label for="product_old1">메인보드</label>&nbsp;
					<input type="checkbox" name="product_old" id="product_old2" value="old02" onclick="onlyOneCheck(this)" /><label for="product_old2">모니터</label>&nbsp;
					<input type="checkbox" name="product_old" id="product_old3" value="old03" onclick="onlyOneCheck(this)" /><label for="product_old3">프린터</label>&nbsp;
					<input type="checkbox" name="product_old" id="product_old4" value="old04" onclick="onlyOneCheck(this)" /><label for="product_old4">하드디스크</label>&nbsp;
					<input type="checkbox" name="product_old" id="product_old5" value="old05" onclick="onlyOneCheck(this)" /><label for="product_old5">메모리</label>&nbsp;
					<input type="checkbox" name="product_old" id="product_old6" value="old06" onclick="onlyOneCheck(this)" /><label for="product_old6">CPU</label>&nbsp;
					<!--token tag"></td>
			</tr>
			<tr>
				<td class="title">
				    <span id="allChoice">
				      <label for="allCheck">모두선택/해제</label><input type="checkbox" id="allCheck" onclick="func_allCheck(this.checked);" />		<!-- ; 있든 없든 상관없다.-->      
				    </span>
				    <br/>품명(미국산)
				</td>
				<td class="data">
				    <input type="checkbox" name="product_usa" id="product_usa1" value="usa01" onclick="func_usaCheck(this.checked)" /><label for="product_usa1">메인보드</label>&nbsp;
					<input type="checkbox" name="product_usa" id="product_usa2" value="usa02" onclick="func_usaCheck(this.checked)" /><label for="product_usa2">모니터</label>&nbsp;
					<input type="checkbox" name="product_usa" id="product_usa3" value="usa03" onclick="func_usaCheck(this.checked)" /><label for="product_usa3">프린터</label>&nbsp;
					<input type="checkbox" name="product_usa" id="product_usa4" value="usa04" onclick="func_usaCheck(this.checked)" /><label for="product_usa4">하드디스크</label>&nbsp;
					<input type="checkbox" name="product_usa" id="product_usa5" value="usa05" onclick="func_usaCheck(this.checked)" /><label for="product_usa5">메모리</label>&nbsp;
					<input type="checkbox" name="product_usa" id="product_usa6" value="usa06" onclick="func_usaCheck(this.checked)" /><label for="product_usa6">CPU</label>&nbsp;
				</td>
			</tr>
			<tr align="center">
				<td colspan="2" class="bordernone">
					<button type="button">쓰기</button>&nbsp;&nbsp;
					<button type="button">취소</button>
				</td>
			</tr>
		</table>
	</div>

</body>
</html>

JS

// === 체크박스 여러개 중 라디오 처럼 1개만 선택되도록 만든 것 === //
function onlyOneCheck(obj){
    const checkbox_list = document.querySelectorAll("input[name='product_old']");

    for(let checkbox of checkbox_list){
        // checkbox != obj 은 체크박스에 체크를 하지 않은 나머지 모든 체크박스를 말한다.
        if(checkbox != obj){
            checkbox.checked = false;
        }
    }   // end of for~of--------------------
}   // end of function onlyOneCheck(obj)----------------------

// === 체크박스 전체선택/전체해제 === //
function func_allCheck(bool){
    const checkbox_list = document.querySelectorAll("input[name='product_usa']");

    for(let checkbox of checkbox_list){
 
        checkbox.checked = bool;    // 체크박스의 체크유무를 동일하게 지정
    
    }   // end of for~of-----------------
}   // end of function func_allCheck(bool)----------------------

// === 체크박스 전체선택/전체해제 에서 
//     하위 체크박스에 체크가 1개라도 체크가 해제되면 체크박스 전체선택/전체해제 체크박스도 체크가 해제되고
//     하위 체크박스에 체크가 모두 체크가 되어지면 체크박스 전체선택/전체해제 체크박스도 체크가 되어지도록 하는 것 === //
function func_usaCheck(bool){

    // 미국산 체크박스 6개중 클릭한 체크박스가 체크가 해제 되어진 상태로 넘어온 경우 
    // if(!bool){
    // 또는
    if(bool == false){
        document.querySelector("input#allCheck").checked = false;
        // 또는 input[id='allCheck']
    }
    // 미국산 체크박스 6개중 클릭한 체크박스가 체크가 되어진 상태로 넘어온 경우 
    else{

        const checkbox_list = document.querySelectorAll("input[name='product_usa']");

        let is_all_checked = true;

        for(let checkbox of checkbox_list){
            // 미국산 체크박스 6개를 반복할때, 해당 체크박스가 체크가 해제 되어진 경우라면
            if(!checkbox.checked){
                is_all_checked = false;
                break;
            }
        }   // end of for------------

        // 미국산 체크박스 6개를 반복할때, 해당 체크박스가 체크가 해제 되어진 경우라면
        if(is_all_checked){      
            document.querySelector("input[id='allCheck']").checked = true;
        }
    }

}   // end of function func_usaCheck(bool)-----------------------

CSS

@charset "UTF-8";

	#underline {text-decoration: underline;
	         	margin-top: 2%;
	         	margin-bottom: 2%;
	         	color: green;}
	
	#allChoice {font-size: 8pt;
	            font-weight: normal;
	            font-style: italic;
	            color: blue;}
	
	.title {width: 18%;
		    background-color: ivory;
	        font-weight: bold;
	        text-align: right;
	        color: #ff6600; }
	
	.data {padding-left: 2%;}        
	
	.bordernone {border: none;}
	
	.h2class {color: blue;}
	
	h2 {color: red;}
	
	table {width: 52%;
		   border: 1px solid red;
	       border-collapse: collapse;
	       border: none; 
	       }
	
	td {border: 1px solid gray;
	    border-collapse: collapse; 
	    padding: 1%;}
	       	   
	tr {line-height: 200%;} 
	
	input {line-height: 150%;} 
	
	button {width: 60px;
	        height: 30px;
	        font-size: 12pt;
	        font-weight: bold; 
	        margin-top: 5%;
	        cursor: pointer;}

정리

  • 12_checkbox
    -> 01_checkbox.html, 01.js, 01.css

0개의 댓글

관련 채용 정보