2023. 7. 28. 15:48ใjquery
Form?
์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๊ธฐ ์ํ ์๋จ
Query๋ ํผ์ ์ ์ดํ๋๋ฐ ํ์ํ ์ด๋ฒคํธ์ ๋ฉ์๋๋ฅผ ์ ๊ณต
ex1)
.focus() : ์ฃผ๋ชฉ ๋ฐ์ผ๋ฉด
.blur() : ํฌ์ปค์ค์ ๋ฐ๋์ ๊ฐ๋
.change() : ์ ๋ ํฐ๋ฅผ ์ ํ์ฌ ํด๋น ์ ๋ ํฐ์ ๊ฐ์ด ๋ณํ ๊ฒฝ์ฐ ๋ณํ๋ฅผ ์บ์น
.select() : ์ด๋ค ํน์ ํ text ํ๋ ์์์ ์ด๋ค ๋ฐ์ดํฐ๊ฐ ์ ํ(๋๋๊ทธ) ๋์ ๋
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>
<input type="text" />
<span></span>
</p>
<script>
//์์ 1 .focus(), .blur(), .change(), .select()
$("input").focus(function(){
//๋ํผ์ ๋ค์ ์๋ฆฌ๋จผํธ ์ฐพ์
$(this).next("span").html('focus');
//chain > .์ผ๋ก ๋ํผ ์ ์งํ์ฌ ์ด๋ฒคํธ ๊ณ์ ๋ฐ์
}).blur( function() {
$(this).next("span").html('blur');
//chain > .์ผ๋ก ๋ํผ ์ ์งํ์ฌ ์ด๋ฒคํธ ๊ณ์ ๋ฐ์
}).change(function(e){
//ํ๊ฒ์ value ๊ฐ ๊ฐ์ด ๋์ (์ด๋ฒคํธ๊ฐ ํธ์ถ๋ ๋ ์ด๋ฒคํธ ์ค๋ธ์ ํธ๊ฐ ์ ๋ฌ ๋จ. > ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์๋ฆฌ๋จผํธ ๋ฆฌํด)
alert('change!! '+$(e.target).val());
//chain > .์ผ๋ก ๋ํผ ์ ์งํ์ฌ ์ด๋ฒคํธ ๊ณ์ ๋ฐ์
}).select(function(){
$(this).next('span').html('select');
});
</script>
</body>
</html>
focus ์ด๋ฒคํธ : input ํ๊ทธ ํด๋ฆญ ์ input ํ๊ทธ ๋ค์ ํ๊ทธ focus html ์ถ๊ฐ
blur ์ด๋ฒคํธ : input ํ๊ทธ ํฌ์ปค์ค ํด์ ์ input ํ๊ทธ ๋ค์ ํ๊ทธ blur html ์ถ๊ฐ
change ์ด๋ฒคํธ : ๋น์ด ์๋ input ํ๊ทธ์ text ์ ๋ ฅ (๋ณํ ์์ ์) alert์ฐฝ ๋์ฐ๊ธฐ
select ์ด๋ฒคํธ : input ํ๊ทธ text ์ ํ(๋๋๊ทธ) ์ select html ์ถ๊ฐ
ex2)
.submit()์ ์ด์ฉํ์ฌ ํผ ์ ์ก์ ์ ์ด
<!DOCTYPE html>
<html>
<head>
<style>
p {
margin:0;
color:blue;
}
div, p {
margin-left:10px;
}
span {
color:red;
}
</style>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>
Type 'correct' to validate.
</p>
<form action="javascript:alert('success!');">
<div>
<input type="text" />
<input type="submit" />
</div>
</form>
<span></span>
<script>
//์์ 2 .submit(), .val()
$("form").submit( function() {
if ($("input:first").val() == "correct") {
//.show() : element๊ฐ ๊ฐ์ถฐ์ ธ์์ ๋ ํ์ ๋๊ฒ
$("span").text("Validated...").show();
//์ด๋ฒคํธ ์คํ ํ submit ์ ์ ์๋
return true;
}
//์ ๋๋ฉ์ด์
ํจ๊ณผ
$("span").text("Not valid!").show().fadeOut(1000);
//์ด๋ฒคํธ ์คํ ํ submit ์๋ X
return false;
});
</script>
</body>
</html>
submit ์ด๋ฒคํธ
correct ์ ์ ์ ๋ ฅ ์ submit ์ด๋ฒคํธ true ๋ฆฌํด > form ํ๊ทธ์ action์ผ๋ก ๊ฐ ์ ๋ฌ ํ alert์ฐฝ ๋์
์์ ๊ฒฝ์ฐ๊ฐ ์๋ ๋๋ submit ์ด๋ฒคํธ false ๋ฆฌํด > ์ด๋ฒคํธ ์๋ X
'jquery' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
animation (0) | 2023.07.28 |
---|---|
traversing(ํ์) (0) | 2023.07.28 |
Mainpulation(์๋ฆฌ๋จผํธ ์ ์ด) (0) | 2023.07.28 |
event (0) | 2023.07.28 |
Chain (0) | 2023.07.27 |