2016-06-20 77 views
0

我試圖通過PHP變量jQuery的,我已經嘗試使用如何通過PHP變量將jQuery

但它不工作的成功,當我嘗試使用

數據源: insted的

dataSource: [ 
    { childName: "Child1", childId: 1, parentId: 1 }, 
    { childName: "Child2", childId: 2, parentId: 2 }, 
    { childName: "Child3", childId: 3, parentId: 1 }, 
    { childName: "Child4", childId: 4, parentId: 2 } 
] 

它不能顯示的第二選擇,但我覺得在$數據是一樣的原始數據

我的代碼,

<head> 
<meta charset="utf-8"/> 
<title>Kendo UI Snippet</title> 

<link rel="stylesheet"  href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.common.min.css"/> 
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.rtl.min.css"/> 
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.silver.min.css"/> 
<link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.2.607/styles/kendo.mobile.all.min.css"/> 

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
<script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js">   
</script> 
</head> 
<body> 

<input id="parent" /> 
<input id="child" /> 
<?php 
$data = '[ 
    { childName: "Child1", childId: 1, parentId: 1 }, 
    { childName: "Child2", childId: 2, parentId: 2 }, 
    { childName: "Child3", childId: 3, parentId: 1 }, 
    { childName: "Child4", childId: 4, parentId: 2 } 
]'; 
?> 
<script> 
$("#parent").kendoDropDownList({ 
    dataTextField: "parentName", 
    dataValueField: "parentId", 
    dataSource: [ 
    { parentName: "Parent1", parentId: 1 }, 
{ parentName: "Parent2", parentId: 2 } 
    ] 
    }); 

$("#child").kendoDropDownList({ 
cascadeFrom: "parent", 
dataTextField: "childName", 
dataValueField: "childId", 

dataSource: <?php json_encode($data); ?> 
}); 
</script> 

</body> 
</html> 

我不知道我的代碼有什麼問題,請幫我解決問題,anyhelp將不勝感激!謝謝!

+0

的【如何傳遞變量可能的複製和數據從PHP到JavaScript?](http://stackoverflow.com/questions/23740548/how-t鄰通變量和 - 數據 - 從 - PHP到的JavaScript) – John

回答

1

您忘記echo你的PHP變量

<?php echo json_encode($data); ?> 

加,因爲你希望它以JSON格式,你必須分析它也正因爲如此,你必須附上屬性名用雙引號。

你的PHP變量將變爲:

$data = '[ 
    { "childName": "Child1", "childId": 1, "parentId": 1 }, 
    { "childName": "Child2", "childId": 2, "parentId": 2 }, 
    { "childName": "Child3", "childId": 3, "parentId": 1 }, 
    { "childName": "Child4", "childId": 4, "parentId": 2 } 
]'; 

dataSource將是:

dataSource: JSON.parse(<?php echo json_encode($data); ?>) 

其工作:)

 Its working :)

0

這裏的問題是,您試圖直接將字符串解析爲JSON,這樣您將在輸出字符串中同時得到「新行」和其他字符,正如您在此測試中看到的:

http://sandbox.onlinephpfunctions.com/code/3c31cecddd99aee0562d09c84b9a8e5770c3444b

但是你能達到所要傳遞一個正確的格式排列的json_encode功能,而不是字符串像這樣的輸出:

$data_array = array(
    array('childName' => "Child1", 
      'childId' => "1", 
      'parentId' => "1"), 
    array('childName' => "Child2", 
      'childId' => "2", 
      'parentId' => "3"), 
    array('childName' => "Child3", 
      'childId' => "3", 
      'parentId' => "3"), 
    array('childName' => "Child4", 
      'childId' => "4", 
      'parentId' => "4"),   
); 

echo json_encode($data_array); 

/* 
Output: 
[{"childName":"Child1","childId":"1","parentId":"1"}, 
{"childName":"Child2","childId":"2","parentId":"3"}, 
{"childName":"Child3","childId":"3","parentId":"3"}, 
{"childName":"Child4","childId":"4","parentId":"4"}] 
*/ 

編輯:爲@穆罕默德 - mudassir指出ÿ您還需要使用JSON.parse將字符串解析回JSON。

dataSource: JSON.parse(<?php echo json_encode($data_array); ?>)