2

我正在使用https://github.com/DubFriend/jquery.repeater(jquery 转发器)来克隆表单字段。它成功地克隆了该字段,但是,当提交表单时,该字段没有被发送。

所以,到目前为止,我发现data-repeater-item正在发送外部的输入字段。


    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.repeater/1.2.1/jquery.repeater.min.js"></script>

    <?php 
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                print_r($_POST);
            }
    ?>

    <h2>Repeater</h2>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="repeater" method="POST" enctype="multipart/form-data">
        <input type="text" name="full_name">
      <div data-repeater-list>
        <div data-repeater-item>

          <input type="text" name="text-input" value="A"/>

          <select name="select-input">
            <option value="A" selected>A</option>
            <option value="B">B</option>
          </select>


          <input data-repeater-delete type="button" value="Delete"/>
        </div>
      </div>
      <input data-repeater-create type="button" value="Add"/>
      <button type="submit">Submit</button>
    </form>




    <script>
    $(document).ready(function () {
        'use strict';

        $('.repeater').repeater({
                show: function () {
                    $(this).slideDown();
                },
                hide: function (deleteElement) {
                    if(confirm('Are you sure you want to delete this element?')) {
                    $(this).slideUp(deleteElement);
                    }
                }
        });
    });
    </script>
4

1 回答 1

7

实际上,您需要为data-repeater-list. 喜欢

<div data-repeater-list="group-a"> //this will be the name for these repeated fields

在此之后,您提交的数据将如下所示:

Array
(
    [full_name] => John Doe
    [group-a] => Array
        (
            [0] => Array
                (
                    [text-input] => A
                    [select-input] => A
                )

        )

)
于 2019-11-03T07:33:43.257 回答