하루의 일상💜

[JSP] Ajax input 아이디창 입력안할시 문구띄우기 본문

JSP

[JSP] Ajax input 아이디창 입력안할시 문구띄우기

도하루박 2022. 11. 7. 22:16
반응형

input 아이디창 입력시 작성하지 않고 다음으로 넘어가면

'아이디를 입력해주세요' 라는 문구가 나오게 하는 방법이다.

 

ajax는 페이징 처리를 하지 않고 데이터를 가져오는 것이기 때문에 동일한 jsp 파일에 띄어줄 문구를 미리 작성해 놓았다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<input type="text" name="test">
	<p id="result"></p>

</body>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script>
$("input[name='test']").on("blur", getData);

function getData(){
	$.ajax({
		url: "test2.jsp",
		type: "get",
		success: function(result){
			$("#result").html(result);
		}
	})
}
</script>
</html>

//input 태그의 name 중 test 인 것에 getData인 블러 이벤트를 줄것이고

그 아래는 그것을 실행 시켜줄 ajax 코드이다.

반응형