我想實現在this website.問題的示例
這給表格的例子dynatree嵌入的腳本代碼:
<script type="text/javascript">
$(function(){
$("#tree").dynatree({
checkbox: true,
selectMode: 3,
onSelect: function(select, node) {
// Display list of selected nodes
var selNodes = node.tree.getSelectedNodes();
// convert to title/key array
var selKeys = $.map(selNodes, function(node){
return "[" + node.data.key + "]: '" + node.data.title + "'";
});
$("#echoSelection4").text(selKeys.join(", "));
},
// In real life we would call a URL on the server like this:
// initAjax: {
// url: "/getTopLevelNodesAsJson",
// data: { mode: "funnyMode" }
// },
// .. but here we use a local file instead:
initAjax: {
url: "sample-data3.json",
data: { mode: "funnyMode" }
},
onActivate: function(node) {
$("#echoActive").text(node.data.title);
},
onDeactivate: function(node) {
$("#echoActive").text("-");
}
});
$("form").submit(function() {
// Serialize standard form fields:
var formData = $(this).serializeArray();
// then append Dynatree selected 'checkboxes':
var tree = $("#tree").dynatree("getTree");
formData = formData.concat(tree.serializeArray());
// and/or add the active node as 'radio button':
if(tree.getActiveNode()){
formData.push({name: "activeNode", value: tree.getActiveNode().data.key});
}
alert("POSTing this:\n" + jQuery.param(formData));
$.post("<?php echo APP_URL;?>admin/submit_data.php",
formData,
function(response, textStatus, xhr){
alert("POST returned " + response + ", " + textStatus);
}
);
return false;
});
});
</script>
而且裏面<body>
標籤:
<form>
Username: <input type="text" name="userName" />
<br>
<textarea name="comment"></textarea>
<br>
<input type="radio" name="rb1" value="foo" checked> Foo
<input type="radio" name="rb1" value="bar"> Bar
<input type="radio" name="rb1" value="baz"> Baz
<br>
<input type="checkbox" name="cb1" value="John" checked>John
<input type="checkbox" name="cb1" value="Paul">Paul
<input type="checkbox" name="cb1" value="George">George
<input type="checkbox" name="cb1" value="Ringo">Ringo
<br>
<!-- The name attribute is used by tree.serializeArray() -->
<div id="tree" name="selNodes">
</div>
<input type="submit" value="Send data">
而在submit_data.php我哈哈ve
print_r($_POST);
當我選擇多個節點並單擊發送數據時,數據將發佈到submit_data.php。當我通過螢火蟲檢查控制檯標籤下的帖子參數時,我得到以下數據。
comment test
rb1 foo
selNodes restaurant1
selNodes screen1
selNodes screen2
selNodes screen3
userName gaurav
但print_r($_POST)
打印以下數據:
Array
(
[userName] => gaurav
[comment] => test
[rb1] => foo
[cb1] => John
[selNodes] => screen3
)
理想我應該得到selNodes的所有值。但根據我的理解,因爲參數名稱是相同的所有節點selNodes,這就是爲什麼我只有一個值。
我如何獲得所有值?
我對PHP沒有太多瞭解,但是如果您在選擇多個複選框時遇到同樣的問題('cb1'),這不是dynatree的問題。也許你應該嘗試$ HTTP_POST_VARS [「selNodes」](只是猜測一些谷歌的結果)? – mar10
不好意思,$ HTTP_POST_VARS好像不推薦使用,$ _POST應該是 – mar10