serializeArray()
원문 링크
http://api.jquery.com/serializeArray/
개요 : 폼 요소를 names와 values 배열로 인코딩합니다.
- serializeArray()
.serializeArray() 함수는 JSON 문자열 형태로 JavaScript 배열 객체를 만듭니다. 이 함수는 폼 요소 집합을 표현하는 jQuery 객체를 이용합니다. 폼 요소들은 여러가지가 있습니다.
<form>
<div><input type="text" name="a" value="1" id="a" /></div>
<div><input type="text" name="b" value="2" id="b" /></div>
<div><input type="hidden" name="c" value="3" id="c" /></div>
<div>
<textarea name="d" rows="8" cols="40">4</textarea>
</div>
<div><select name="e">
<option value="5" selected="selected">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select></div>
<div>
<input type="checkbox" name="f" value="8" id="f" />
</div>
<div>
<input type="submit" name="g" value="Submit" id="g" />
</div>
</form>.serializeArray() 함수는 W3C 의 표준 규칙인 successful controls 을 따릅니다. 요소는 disabled이 아니어야 하고 반드시 name 속성을 지녀야 합니다. type='file' 요소는 포함되지 않습니다.
.serialize() 함수는 jQuery 객체로 사용할 수 있는데, 폼 요소 개별 객체별로 사용할 수 있습니다. 즉 <input>, <textarea>, <select>와 같은 개별 요소들이 이 함수를 사용할 수 있는 것입니다. 하지만, 대체적으로 아래 예제와 같이 <form> 태그를 선택해서 직렬화 하는 방식이 많습니다.
$('form').submit(function() {
console.log($(this).serializeArray());
return false;
});위 예제는 아래와 같은 결과물을 만들어 내게 됩니다.
[
{
name: "a",
value: "1"
},
{
name: "b",
value: "2"
},
{
name: "c",
value: "3"
},
{
name: "d",
value: "4"
},
{
name: "e",
value: "5"
}
]예 제
폼 요소들로의 value값을 보여줍니다.
<!DOCTYPE html>
<html>
<head>
<style>
body, select { font-size:14px; }
form { margin:5px; }
p { color:red; margin:5px; }
b { color:blue; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p><b>Results:</b> <span id="results"></span></p>
<form>
<select name="single">
<option>Single</option>
<option>Single2</option>
</select>
<select name="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select><br/>
<input type="checkbox" name="check" value="check1" id="ch1"/>
<label for="ch1">check1</label>
<input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/>
<label for="ch2">check2</label>
<input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>
<label for="r1">radio1</label>
<input type="radio" name="radio" value="radio2" id="r2"/>
<label for="r2">radio2</label>
</form>
<script>
function showValues() {
var fields = $(":input").serializeArray();
$("#results").empty();
jQuery.each(fields, function(i, field){
$("#results").append(field.value + " ");
});
}
$(":checkbox, :radio").click(showValues);
$("select").change(showValues);
showValues();
</script>
</body>
</html>미리보기
폼 데이터를 JSON 데이터로 만들 때 사용하는 함수입니다.
그럼 즐프하세요.
※ 본 예제는 http://www.jquery.com 에 있는 내용임을 밝힙니다.
'프로그래밍 > jQuery' 카테고리의 다른 글
| jQuery.inArray(), 배열 내의 값을 찾아서 인덱스를 반환 (0) | 2012.07.02 |
|---|---|
| jQuery.grep(), 배열 요소를 찾아 걸러내기 (0) | 2012.07.02 |
| jQuery.extend(), 두개 이상의 객체를 합치기(Merge) (0) | 2012.07.02 |
| jQuery.each(), 일반적인 반복 함수 (2) | 2012.07.02 |
| serialize(), 폼 요소 집합을 인코딩 (2) | 2012.07.02 |
| jQuery.post(), Ajax HTTP POST 방식 요청 (1) | 2012.07.02 |
| jQuery.param(), Ajax 데이터용 배열이나 객체를 직렬화 (0) | 2012.07.02 |
| load(), Ajax로 받은 HTML을 일치하는 요소 안에 추가 (2) | 2012.07.02 |