如何遍历数组,其中数组是键的值?

时间:2013-11-29 13:06:58

标签: arrays json jquery post

Firebug中的Ajax POST response

{"key_one": "val_1", "key_2": "val_2", "key_3": ["array_val_1", "array_val_2", "array_val_3"]}

如何使用jQuery迭代key_3中的数组项?

1 个答案:

答案 0 :(得分:0)

实际上,我试图解决这个问题,这就是我用过的东西:

<script>
    $(document).on("submit", "#my_div", function(e) {
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: '/my_path',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(results) {
                if (something) {
                    //do this
                } else {
                    // do this
                    $("#content_area").html("");
                    $("#content_area").append(results.key_2);
                    // and for each item in the array, do something with each item in the array
                    my_array = results.key_3;
                    $.each(my_array, function(k, v) {
                        alert(v) // this will alert each value in the array
                    });
                }
            }
        });
    });
</script>