Love beautiful code? We do too.
.serializeArray(): Lấy giá trị các thành phần form, mã hóa các giá trị này thành giá trị mãng.
Giá trị sẽ được hiển thị cách nhau bởi khoảng trắng: value1 value2...
Đã được thêm vào từ phiên bản 1.2
.serializeArray()
$('form').serializeArray();
Html viết:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Tiêu đề</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
function showValues() {
var fields = $(":input").serializeArray();
$("#result").empty();
jQuery.each(fields, function(i, field){
$("#result").append(field.value);
});
}
$(":radio").click(showValues);
showValues();
});
</script>
</head>
<body>
<form>
<input type="radio" name="radio" value="Male" checked="checked" id="male" />
<label for="male">Male:</label>
<input type="radio" name="radio" value="Female" id="female" />
<label for="female">Female:</label>
</form>
<p id="result"></p>
</body>
</html>
Hiển thị trình duyệt:
Khi click chọn vào radio ta đã lấy được giá trị thay đổi của thành phần này.
Hoclaptrinh.vn © 2017
From Coder With
Unpublished comment
Viết câu trả lời