0

我正在尝试在本网站给出的表单示例中实现 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 我有

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 相同,这就是为什么我只得到一个值的原因。

我如何获得所有值?

4

1 回答 1

2

我对 PHP 了解不多,但网络上的一些复选框示例将大括号附加到元素名称以传递数组:

<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

所以你可以试试

<div id="tree" name="selNodes[]">
</div>
于 2011-09-10T07:36:52.217 回答