代码之家  ›  专栏  ›  技术社区  ›  TheOrdinaryGeek

添加动态输入的php&jquery

  •  2
  • TheOrdinaryGeek  · 技术社区  · 7 年前

    我使用jquery在我的页面上添加动态输入。我最初只想显示一个输入,然后单击一个按钮可以添加更多输入。

    这按预期工作。

    然后我使用php来捕获 $_POST 输入值并将其发送到外部脚本。这同样有效,但是我总是在数组中收到一个额外的项目,而且它是空的。

    我想这是因为我隐藏了 <div> “我的HTML”中的字段,在生成新输入时显示该字段?

    我的代码如下;

    HTML

    // unnecessary code removed
    <div class="after-add-more">
      <button class="add-more" type="button" title="Add"></button>
      <input name="addmore[]" value="" type="text">
    </div>
    
    <div class="copy-fields hide">
      <div>
        <button class="remove" type="button" title="Remove"></button>
        <input type="text" name="addmore[]" value="">
      </div>
    </div>
    

    JQuery

    $(document).ready(function() {
        //here first get the contents of the div with name class copy-fields and add it to after "after-add-more" div class.
        $(".add-more").click(function() {
            var html = $(".copy-fields").html();
            $(".after-add-more").after(html);
        });
        //here it will remove the current value of the remove button which has been pressed
        $("body").on("click", ".remove", function() {
            $(this).parents(".control-group").remove();
        });
    });
    

    PHP

    <?php
    // unnecessary code removed
    $field_values_array = $_POST['addmore'];
    ?>
    

    在不生成额外输入的情况下,我输入 1111111 输入框并提交。一 print_r($_POST) 生产;

    [addmore] => Array
        (
            [0] => 1111111
            [1] => 
        )
    

    感谢您的帮助。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Adam H    7 年前

    $(document).ready(function() {
    
      // this function handles the click event
      function addField(parent) {
        // find your template, in this case its the first .after-add-more
        var html = $(".after-add-more").first().clone();
        // reset the value of any inputs
        $('input', html).val('');
        // wire the click handler for the button
        $("button", html).click(function() {
          addField($(this).parent());
        });
        // append it to the parent of the parent, addContainer
        html.appendTo(parent.parent());
      }
    
      // wire the click handler for the add-more button
      $(".add-more").click(function() {
        addField($(this).parent());
      });
      
      // I don't know what the intention of this code is so I'm leaving it alone
      $("body").on("click", ".remove", function() {
        $(this).parents(".control-group").remove();
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    // unnecessary code removed
    <div id="addContainer">
      <div class="after-add-more">
        <button class="add-more" type="button" title="Add">Add</button>
        <input name="addmore[]" value="" type="text">
      </div>
    </div>
    <div class="copy-fields hide">
      <div>
        <button class="remove" type="button" title="Remove">Remove</button>
        <input type="text" name="addmore[]" value="">
      </div>
    </div>