Định nghĩa và sử dụng .serializeArray() trong jQuery

.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ấu trúc .serializeArray() trong jQuery

Đã được thêm vào từ phiên bản 1.2

.serializeArray()

$('form').serializeArray();

.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.

Viết câu trả lời

Drop Images

0 Bình luận