2014-04-14 44 views
0

这是我想要完成的。从php中插入一个数组到一个HTML表单中,然后将这个数组推入一个JS数组中。数组从PHP到HTML输入到JS

PHP

 $proxiearray = array();    
     if($row7['attribute_0'] != NULL) { array_push($proxiearray,$row7['attribute_0']);} 
     if($row7['attribute_1'] != NULL) { array_push($proxiearray,$row7['attribute_1']);} 

     <input type="hidden" class="proxie_attributes" value="'.htmlspecialchars(json_encode($proxiearray)).'"> 

现在我想的是什么是对uppon在客户端有一定的任务为我的JS拿那个阿雷和另外一个推动它。

JS

attributesArray[counter] = new Array(); 
if(proxiearray) { attributesArray[counter] = proxiearray; } 

如果我提醒阵列我得到 ["1","4"],5,6

这只是把数组中的["1","4"],但我希望它推的是有像 1,4,5,6

结果

我知道我可以使用X数量的输入,尽管这不是一个解决方案,因为我可能不得不在未来使用更多的属性。编辑:alertattributesArrayattributesArray[counter]提醒是["1","4"]

+0

什么是计数器在'attributesArray [counter] = new Array();'? –

+0

属性数组内部的数组。编辑问题 – LefterisL

回答

0

您可以使用以下;

if ($(this).find('div .proxie_attributes').length > 0) { 
    var proxiearray = JSON.parse($(".proxie_attributes").val()); 
    attributesArray[counter] = new Array(); 
    if(proxiearray) { 
     attributesArray[counter] = proxiearray; 
    } 
} 
+0

'var proxiearray = JSON.parse($(this).find('div .proxie_attributes')。val());'但是一个问题,如果产品不存在'.proxie_attributes'由于某些原因JS停止 – LefterisL

+0

所以你需要检查。看到我更新的答案 –