从动态文本框获取值


Get Values from dynamic textbox?

我有这个代码来创建一个文本框,每个文本框都有自己的唯一 id,例如文本框 1、文本框 2 等。我在提交数据时遇到了麻烦,因为文本框是动态的,你不会知道用户创建了多少文本框,所以我不知道如何发布数据,例如 $_POST['textbox'],。

$(document).ready(function(){
    var counter = 2;
    $("#addButton").click(function () {
    if(counter>10){
            alert("Only 10 textboxes allow");
            return false;
    }   
    var newTextBoxDiv = $(document.createElement('div'))
         .attr("id", 'TextBoxDiv' + counter);
    newTextBoxDiv.after().html('<input type="text" name="txtLine' + counter + '" id="txtLine' + counter + '" placeholder="Line#' + counter +' " >');
    newTextBoxDiv.appendTo("#TextBoxesGroup");

    counter++;
     });

您可以像这样提交带有方括号的表单元素,它会将所有表单输入作为数组发布:

<form method="post" action="form.php">
  <input name="txtLine[]" />
  <input name="txtLine[]" />
  <input name="txtLine[]" />
  <input type="submit" />
</form>

然后,您可以在数组中访问所有这些,如下所示$_POST["txtLine"]

$_POST输出:

Array
(
    [txtLine] => Array
        (
            [0] => hey
            [1] => there
            [2] => jack
        )
)