IT/jQuery

jQuery - form submit 이벤트 함수, event.preventDefault()

노마드오브 2018. 11. 17. 19:44

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

$(document).ready(function () {

// 폼 submit() 이벤트함수

// var name = 

// var passwd = 

// 경고창 name:passwd

// submit 기본기능을 못하게 막아줌

$('#my_frm').submit(function (event) {

var name = $('#name').val();

var passwd = $('#passwd').val();

alert(name + ' : ' + passwd);

if (name.length == 0 || passwd.length == 0) {

alert('이름과 패스워드를 모두 입력해주세요');

//return false;

event.preventDefault();

}

});

});

</script>

</head>

<body>

<form action="loginPro.jsp" id="my_frm">

이름: <input type="text" name="name" id="name"><br>

패스워드: <input type="password" name="passwd" id="passwd"><br>

<input type="submit" value="로그인">

</form>

</body>

</html>