[jQuery] each 를 이용한 json 데이터 처리
DB 등에 저장된 설정 데이터를 ajax json 데이터로 가져와서 입력폼의 input 등에 적용하는 개념적인 코드이다.
$(".config_load").on("click", function() {
    var $el;
    var type = "config";
    $.ajax({
        type: "POST",
        url: "./config_load.php",
        cache: false,
        async: false,
        data: { type: type },
        dataType: "json",
        success: function(data) {
            if(data.error) {
                alert(data.error);
                return false;
            }
            $.each(data, function(key, val) {
                if(key == "error")
                    return true;
                $el = $("#"+key);
                if($el[0].type == "checkbox") {
                    $el.attr("checked", parseInt(val) ? true : false);
                    return true;
                }
                $el.val(val);
            });
        }
    });
});json 데이터를 $.each(data, function(key, val) { 부분에서 key, value 로 구분하여 key 값이 id 인 엘리먼트에 적용해주는 것이다. checkbox 는 별도 처리를 하고 있으며 radio 역시 별도 처리를 해야하지만 코드에서는 빠져있다.