-1

我已经上传了一段我的 JS 代码(http://jsfiddle.net/3mcm2/),它的最底部是我在我的 PHP 文档中调用 JS 的方式。为了运行脚本,只需从底部删除 PHP 代码注释。我只是想为您添加它,以了解我如何在 PHP 中输​​出它。此外,在最后的注释上方是 .js 文件中的三行注释,您可以在其中查看 PHP 正在回显的内容,以帮助您更好地了解所有内容的外观。

/* The following is what is in my .js file: (see the bottom of this script for part of         
   what is in my PHP file) */

var f = document.createElement("form");
f.setAttribute('method', "get");
f.setAttribute('action', "index.php");

var Category = (function () {
    var categoryCount = 0;

    function elem(tag) { // shortcut
        return document.createElement(tag);
    }

    function text(str) { // shortcut
        return document.createTextNode(str);
    }

    function Category(node) {
        var self = this;
        this.categoryId = ++categoryCount;
        // make add button
        this.addButton = elem('button');
        this.addButton.appendChild(text('Add Textbox'));
        this.addButton.addEventListener('click', function () {
            self.addTextbox();
        });
        // make wrapper
        this.wrapper = elem('section');
        this.wrapper.setAttribute('id', 'cat'+this.categoryId);
        this.wrapper.appendChild(this.addButton);
        // make textboxes
        this.textboxes = [];
        this.addTextbox();
        // append to document
        if (node) {
            this.append(node);
        }

    }

    Category.prototype.addTextbox = function () {
        var e = document.createElement("input");
        e.setAttribute('name', 'cat-'+this.categoryId+'-textbox[]');
        f.appendChild(e); // this is where each textbox is supposed to be added to the form...
        this.textboxes.push(e);
        this.wrapper.insertBefore(e, this.addButton);
    };

    Category.prototype.append = function (node) {
        return node.appendChild(this.wrapper);
    };

    return Category;
}());

var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"submit");
s.setAttribute('value',"Submit");
f.appendChild(s);

//var cat1 = new Category(document.body);
//var cat2 = new Category(document.body);
//document.getElementsByTagName('body')[0].appendChild(f);

上面的注释仅供您查看此脚本在做什么,而这三行实际上不在我的 .js 文件中。以下评论是我的 PHP 文件中的一部分,几乎只是输出上述评论:

$counter = 0;
echo '<script type="text/javascript" src="js/categories.js"></script>';

foreach ($catArr as $category) {
    $counter++;
    echo 'do<script>var cat'.$counter.' = new Category(document.body);</script>';
}

echo "<script>document.getElementsByTagName('body')[0].appendChild(f);</script>";

我的问题是,对于我在 JS 中创建的表单,GET 没有传递任何数据。我的 php 页面只是从 index.php 到 index.php?问号,而不是问号后面的任何文本框变量。出于某种原因,表单没有找到那些创建的文本框或其名称。请帮帮我。

4

1 回答 1

0

这段代码:

var cat1 = new Category(document.body);
function Category(node) {
        var self = this;
        this.categoryId = ++categoryCount;
        // make add button
        this.addButton = elem('button');
        this.addButton.appendChild(text('Add Textbox'));
        this.addButton.addEventListener('click', function () {
            self.addTextbox();
        });
        // make wrapper
        this.wrapper = elem('section');
        this.wrapper.setAttribute('id', 'cat'+this.categoryId);
        this.wrapper.appendChild(this.addButton);
        // make textboxes
        this.textboxes = [];
        this.addTextbox();
        // append to document
        if (node) {
            this.appendChild(node);
        }

    }

将所有输入文本框附加到body而不是form,因此当您按下提交时,不会传递任何数据

摆弄工作代码:http: //jsfiddle.net/5H8Pv/1/

于 2013-07-03T18:09:34.947 回答