본문 바로가기

JavaScript&JQuery

자주쓰는 jquery 함수 모음

 

▣ 체크박스 전체선택 스크립트

<script>
    // 전체 선택 
    $("#selectAll").click(function(){
        if($(this).prop("checked")){
            $(".checkbox").prop("checked", true);
        } else {
            $(".checkbox").prop("checked", false);
        }
    });

    // 체크박스
    $(".checkbox").click(function(){
        if(!$(this).prop("checked")){
            $("#selectAll").prop("checked", false);
        }
    });
</script>

<input type="checkbox" id="selectAll"> 전체 선택<br>
<input type="checkbox" class="checkbox"> 항목 1<br>
<input type="checkbox" class="checkbox"> 항목 2<br>
<input type="checkbox" class="checkbox"> 항목 3<br>

 

 

 

▣ 공백입력 막기 스크립트

<script>
	function noSpaceForm(obj) { 
		var str_space = /\s/; 
		if(str_space.exec(obj.value)) { 
			obj.value = obj.value.replace(' ',''); 
			return false;
		}	 
	}
</script>   

<input type="text" name="no" id="no" onkeyup="noSpaceForm(this);" onchange="noSpaceForm(this);">

 

 

 

▣ 핸드폰번호 포맷 설정

<script>
	//핸드폰번호 포맷
	function formatPhoneNumber(input) {
		let phoneNumber = input.value;
		phoneNumber = phoneNumber.replace(/\D/g, '');
		if (phoneNumber.length > 11) {
			phoneNumber = phoneNumber.slice(0, 11); // 11자를 초과하면 자름
		}
		if (phoneNumber.length >= 3 && phoneNumber.length < 7) {
		  phoneNumber = phoneNumber.replace(/(\d{3})(\d{1,3})/, '$1-$2');
		} else if (phoneNumber.length >= 7) {
		  phoneNumber = phoneNumber.replace(/(\d{3})(\d{4})(\d{1,4})/, '$1-$2-$3');
		}
		input.value = phoneNumber;
	}
</script>

<input type="text" name="hp" id="hp" oninput="formatPhoneNumber(this)">

 

 

▣ 조사 구분 스크립트

<script>
	function getName(word) {
	  if (typeof word !== 'string') return null;
	 
	  var lastLetter = word[word.length - 1];
	  var uni = lastLetter.charCodeAt(0);
	 
	  if (uni < 44032 || uni > 55203) return null;
	 
	  return (word + (((uni - 44032) % 28 != 0) ? '을':'를'));
	}
    
    console.log(getName(text));
</script>