【问题标题】:How to iterate over an array where the array is the value of a key?如何迭代一个数组,其中数组是键的值?
【发布时间】:2013-12-15 16:46:06
【问题描述】:

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 中的数组项?

【问题讨论】:

    标签: arrays json jquery post


    【解决方案1】:

    实际上,我在尝试解决这个问题时得到了这个工作,这就是我使用的:

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

    【讨论】:

      猜你喜欢
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      相关资源
      最近更新 更多