【问题标题】:Dynamically add html depending on dynamically generated html with jQuery根据使用jQuery动态生成的html动态添加html
【发布时间】:2016-09-13 20:35:36
【问题描述】:

我目前正在开发一个项目,我必须为此开发一个允许操作数据库中的表的 WordPress 插件。我的工作基于这个插件,我正在改进和编辑它以满足我公司的要求:https://wordpress.org/plugins/create-db-tables/

然而,我的问题与 MySQL 无关,而是与 html、Php 和 jQuery(尤其是 jQuery,我根本不熟悉)有关。它是关于动态生成一个表单,它本身取决于动态生成的表单(是的......)。

使用以下代码,插件用户能够动态地将行添加到他正在创建的表中。我希望他能够添加“子行”(我知道如何在服务器端处理它,我的问题只是界面),就像这样:

http://i.imgur.com/XHwI54W.png

当用户单击“添加子行”时,会出现一个区域,他可以在其中填写表格等。第一行(至少是界面)完美运行,但之后,每当我单击另一个“添加子行”时" 按钮,没有任何反应。

如果有人能花时间告诉我我做错了什么,我将不胜感激。太感谢了 ! :)

这是我的代码: (第一个 jQuery 脚本似乎有效,但我不是开发它的人。我复制了它并尝试使第二个脚本适应我的“子行”要求)

<form id="create-table" method="post" action="<?php echo admin_url('admin-post.php'); ?>">

        <input type="hidden" name="action" value="add_table">

        <fieldset class="table-fieldset">
            <label id="table-name">Table Name:</label> <br />
            <span style="position: relative; top: 2px;"><?php echo "Choose prefix (by default wp_ ) : " ?></span><input type="text" class="prefix-name" name="prefix_name" size="4" id="prefix-name"><br />
            <span style="position: relative; top: 2px;"><?php echo "Choose table name :" ?></span><input type="text" class="table-name" name="table_name" size="30" id="table-name">
            <span>(Alphanumeric only, no special characters.)</span> <br/> <br/>
            <span><font color="blue"><strong>Will this table be used for user authentication ? </strong></font></span><input type="checkbox" class="is-user-auth" name="is_user_auth" id="is-user-auth"><br/>
            <span><strong>/!\ Only one table at a time can be used for user authentication : if a current table is used for this purpose, it won't be anymore.</strong></span><br/><br/>
        </fieldset>

        <div id="rows">
            <fieldset class="row-fieldset" id="row-fieldset" value="1"><label id="row-label">Row:</label><input type="text" class="name-input" name="name[]" placeholder="Name" size="20"><input type="text" class="type-input" name="type[]" placeholder="Type [Ex: bigint(20)]" size="20"><span id="null-label">Null</span><input type="checkbox" class="null-input" name="null[]"><input type="text" class="default-input" name="default[]" placeholder="Default Value" size="20"><span id="unique-label">Unique</span><input type="checkbox" class="unique-input" name="unique[]"><button type="button" class="row-dynamic" style="background-color: #E3F6CE">Add subrows</button><div id="subrows1" value="1"></div></fieldset>
        </div>

        <div id="add-row">
            <button type="button" class="add-row button-secondary">Add Row</button>
        </div>

        <fieldset>
            <input type="hidden" id="items" name="items" value="1" />
        </fieldset>

        <fieldset>
            <input type="hidden" id="subrowitems" name="subrowitems" value="101" />
        </fieldset>

        <fieldset>
            <button type="submit" class="button button-primary button-large">Create Table</button>
        </fieldset>

    </form>

    <!-- Script to add rows -->
    <script>
        jQuery(function($) {
            $('.add-row').click(function () {
                $('#items').val(function(i, val) { return +val+1 });
                var val_2=$('#items').attr('value');
                var subrow_name = 'subrows'+val_2;
                var rowHTML = '<fieldset class="row-fieldset" id="row-fieldset" value=\"'+val_2+'\"><label id="row-label">Row:</label><input type="text" class="name-input" name="name[]" placeholder="Name" size="20"><input type="text" class="type-input" name="type[]" placeholder="Type [Ex: bigint(20)]" size="20"><span id="null-label">Null</span><input type="checkbox" class="null-input" name="null[]"><input type="text" class="default-input" name="default[]" placeholder="Default Value" size="20"><span id="unique-label">Unique</span><input type="checkbox" class="unique-input" name="unique[]"><button type="button" class="row-dynamic" style="background-color: #E3F6CE">Add subrows</button><div id=\"'+subrow_name+'\" value=' + val_2 + '></div></fieldset>';
                $('#rows').append(rowHTML);
            });
            $("input.name-input").on({
                keydown: function(e) {
                    if (e.which === 32)
                        return false;
                },
                change: function() {
                    this.value = this.value.replace(/\s/g, "");
                }
            });
            $("input.table-name").on({
                keydown: function(e) {
                    if (e.which === 32)
                        return false;
                },
                change: function() {
                    this.value = this.value.replace(/\s/g, "");
                }
            });
        });
    </script>

    <!-- Script to add subrows -->
    <script>
        jQuery(function($) {
            $('.row-dynamic').click(function () {
                $('#subrowitems').val(function(i, val) { return +val+1 });
                var val_3=$('#subrowitems').attr('value');
                var subrowHTML = '<fieldset class="subrow-fieldset" value=\"' +val_3+ '\"><label id="subrow-label">Subrow:</label><input type="text" class="subrow-name-input" name="name[]" placeholder="Name" size="20" style="background-color: #E3F6CE"><input type="text" class="subrow-type-input" name="type[]" placeholder="Type [Ex: bigint(20)]" size="20" style="background-color: #E3F6CE"><span id="subrow-null-label">Null</span><input type="checkbox" class="subrow-null-input" name="null[]" style="background-color: #E3F6CE"><input type="text" class="subrow-default-input" name="default[]" placeholder="Default Value" size="20" style="background-color: #E3F6CE"><span id="subrow-unique-label">Unique</span><input type="checkbox" class="subrow-unique-input" name="unique[]" style="background-color: #E3F6CE"></fieldset>';
                var subrow_val = '#subrows'+$('#row-fieldset').attr('value');
                $(subrow_val).append(subrowHTML);
            });
            $("input.subrow-name-input").on({
                keydown: function(e) {
                    if (e.which === 32)
                        return false;
                },
                change: function() {
                    this.value = this.value.replace(/\s/g, "");
                }
            });
            $("input.table-name").on({
                keydown: function(e) {
                    if (e.which === 32)
                        return false;
                },
                change: function() {
                    this.value = this.value.replace(/\s/g, "");
                }
            });
        });
    </script>

【问题讨论】:

    标签: javascript php jquery html dynamic


    【解决方案1】:

    您的 HTML sintax 中有一些重复的 id(例如 #row-fieldset

    如果您检查您的 HTML 代码,您将使用行字段集 id 初始化一个 fieldset 标记

    <fieldset class="row-fieldset" id="row-fieldset" value="1">
    

    然后你用同样的id创建一个新的

    var rowHTML = '<fieldset class="row-fieldset" id="row-fieldset" value=\"'+val_2+'\
    

    所以当 JQuery 试图找到正确的id 时,只找到第一个。

    我在 Add subrows 按钮中添加了一个值,用于定位正确的行

    另外你没有提供任何 jQuery 版本,所以我使用 2.2.0

    检查此fiddle 以查看其运行情况

    希望对你有帮助

    【讨论】:

    • 非常感谢您的回答,您的代码在我的网站上运行良好(我使用 jQuery 2.2.3,但基本相同)。现在我明白我的错误,我会尽量不重复它们。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2011-12-02
    相关资源
    最近更新 更多