jQuery和JSON:循环JSON数组


jQuery and JSON: loop json array

我在数据库中有一个数组:

a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}

用php反序列化,然后用json编码,代码如下

$unwp = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
print_r ($unwp);
echo json_encode($unwp);

我在页面上得到这个:

Array ( [1] => 1993 [2] => 1994 [3] => 1995 [4] => 1996 ) {"1":"1993","2":"1994","3":"1995","4":"1996"}

我需要用jQuery循环它?所以我可以得到1993,1994,1995,1996等等。

我正在测试jQuery.getJSON(),但不知道如何确切地使用它?

页面上的所有代码:

<?php
   $array = $_POST['inputarray'];
   $str = serialize($array);
   print $str . "'n";
   $unwp = unserialize('a:4:{i:1;s:4:"1993";i:2;s:4:"1994";i:3;s:4:"1995";i:4;s:4:"1996";}');
   print_r ($unwp);
   echo json_encode($unwp);
?>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
   jQuery(function ($) {
    // Add children input fields
    var childBlock = $('.block');
    var countChildren = $('div.block div.row').size() + 1;
    $('#addChild').live('click', function (e) {
        e.preventDefault;
        $('<div class="row"><input type="text" name="inputarray['+countChildren+']" id="inputarray'+countChildren+'" placeholder="inputarray['+countChildren+']"><a href="javascript://" id="deleteChild">Delete</a></div>').appendTo(childBlock);
        countChildren++;
    });
    $('#deleteChild').live('click', function (e) {
        e.preventDefault();
        if (countChildren > 2) {
            $(this).parents('div.row').remove();
            countChildren--;
            var counter = 1;
            $('input[name^="inputarray"]').each(function () {
                $(this).attr('name', 'inputarray[' + counter + ']');
                $(this).attr('placeholder', 'inputarray[' + counter + ']');
                $(this).attr('id', 'inputarray' + counter);
                counter++;
            });
        }
    });
})(jQuery);
</script>

   <form action="" method="post">
      <div class="block">
         <div class="row"><input type="text" name="inputarray[1]" placeholder="inputarray[1]"></div>
         <input type="hidden" value="<?php echo $str; ?>">
      </div>
      <input type="submit">
   </form>

<a href="javascript://" id="addChild">Add a child</a>

谢谢!

这在PHP中很容易做到。因为我没有看到submit()click()或任何可能建议ajax请求的处理程序。

在同一个文件中也有php,那么为什么不简单地循环php并生成所需的内容呢?

echo "<select name='year'>";
foreach($unwp as $year) {
    echo "<option value='{$year}'>{$year}</option>";
}
echo "</select>";

上面的代码片段将产生你所需要的东西。

示例


编辑

你想生成一个<select>对吗?如果没有,请告诉我,以便我根据需要修改。

我会改变

回声json_encode ($ unwp);

echo "<script> var fromPhP = ".json_encode($unwp). "</script>;

以这种方式你得到json变量,我看到你正在使用jquery,所以我将使用$。每个循环它:

$.each(fromPhP ,function(index,item){
    console.log(index,item);
});

使用$.getJSON:

$.getJSON("scriptname.php", function(data) {
    html = '';
    $.each(data, function(i, el) {
        html += '<div class="block">' +
            '<div class="row"><input type="text" name="inputarray['+i+']" placeholder="inputarray['+i+']"></div>' +
            '<input type="hidden" value="'+el+'">');
    });
    $("form > div").delete();
    $("form").prepend(html);
});