๋ชฉ์ฐจ

JavaScript onsubmit ๐
๋ฏธ๋์ ๋ด๊ฐ ๋ณด๊ธฐ ์ํด ์์ฑ
ํ๋ฉด์์ submit์ ์ปจํธ๋กค ํด์ผ ํ ๊ฒฝ์ฐ๊ฐ ์๋ค.
์๋ฅผ ๋ค๋ฉด ์ ๋ ฅ๊ฐ์ ์ ๋ ฅ ์ํ๋ฉด ๋ชป ๋์ด๊ฐ๊ฒ ํ๋ค๊ฑฐ๋..
์ฌ๋ฌ ๋ฐฉ๋ฒ์ด ์๊ฒ ์ง๋ง form์ onsubmit ์ต์ ์ ์ฃผ๊ณ ํจ์๋ฅผ ์ง์ ํด ์ฃผ๋ฉด submit ์ ๋ฌด์กฐ๊ฑด ํด๋น ํจ์๋ฅผ ํธ์ถํ๊ณ submitํ๊ฒ ๋๋ค.
๊ทธ๋ฆฌ๊ณ return functionName(); ์ ์ฃผ๊ฒ ๋๋ฉด ํด๋น ํจ์์ return๊ฐ์ด false์ธ ๊ฒฝ์ฐ submit์ด ์๋๋ค.
๊ทธ๋์ ์๋ ์์์ฒ๋ผ ์กฐ๊ฑด์ ๋ฐ๋ผ submit์ ํ๊ฑฐ๋ ๋ง์ ์ ์๋ค.
์๋ ์์๋ ์ ๋ชฉ์ ์ ๋ ฅ ์ํ ๊ฒฝ์ฐ submit์ด ์๋๋ค.(๋๋ฌด ๋จ์ํ ์ ํจ์ฑ ์ฒดํฌ์ด์ง๋ง..)
์์ ์ฝ๋
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>onsubmit test</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<form action="board/new" method="post" onsubmit="return check();">
<div class="form-group">
<label for="title"></label>
์ ๋ชฉ: <input id="title" type="text" name="title"> <br>
๋ด์ฉ: <input id="contents" type="text" name="contents"> <br>
์์ฑ์: <input id="writer" type="text" name="writer"> <br>
</div>
<button type="submit">์ ์ก</button>
</form>
<script>
$(document).ready(function() {
});
function check() {
let title = $("#title").val();
if(title === "" || title === undefiend || title === " ") {
alert("์ ๋ชฉ์ ํ์๊ฐ์
๋๋ค. ์
๋ ฅ ์ํ๋ฉด ๋์ด๊ฐ ์ ์์ต๋๋ค.");
return false;
}
}
</script>
</body>
</html>
์ด๋ฅผ ์ด์ฉํด์ ๋ค์ํ๊ฒ ์์ฉ์ด ๊ฐ๋ฅํ๋ค.

'IT > development' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [mybatis] ์์ธ ์กฐํ ์ ๋ณต์๊ฐ ์ถ๊ฐ ์กฐํ(feat. vo & map) (0) | 2022.11.22 |
|---|---|
| [JavaScript] Ajax ๊ฒฐ๊ณผ๊ฐ ๋ณ์ ์ ์ฅ (0) | 2022.11.22 |
| [spring Boot] Request method 'POST' not supported(feat. thymeleaf) (0) | 2022.11.22 |
| [jQuery] jQuery.ajax() ์ฌ์ฉ๋ฒ ์์(feat. ๋๊ธ ํ์ด์ง๋ค์ด์ ) (0) | 2022.11.21 |
| [IDE] intellJ IDEA live template ์์ฑ (0) | 2022.11.21 |
๋๊ธ