允许用户使用jquery创建和提交多达5个文本框,并在php中将它们解析为一个数组

Allow user to create and submit up to 5 text boxes with jquery, and parse them into one array in php?

本文关键字:并在 php 一个 数组 文本 jquery 用户 许用户 创建 5个 提交      更新时间:2023-09-26

有可能吗?我希望用户发布一个数组,其中包含1-5条数据。

起初,显示的文本字段只有一个,但单击旁边的"加号"图标后,它会在下面创建另一个文本字段,以供更多用户输入。

我还想在文本框2-5旁边有一个删除图标,以便在必要时删除它们。

我的JQuery知识有限,我可以想出如何将文本框附加到列表中,但不能跟踪/删除它们。理想情况下,我还想将它们作为数组传递给php,这样我就可以轻松地循环使用它们。

<input type="text" size="15" maxlength="15" name="1"><img src="add.png" onclick="add();">
<!-- Below is hidden by default, and each one shows on click of the add image -->
<input type="text" size="15" maxlength="15" name="2"><img src="delete.png" onclick="delete(2);">
<input type="text" size="15" maxlength="15" name="3"><img src="delete.png" onclick="delete(3);">
<input type="text" size="15" maxlength="15" name="4"><img src="delete.png" onclick="delete(4);">
<input type="text" size="15" maxlength="15" name="5"><img src="delete.png" onclick="delete(5);">
jQuery clone()非常方便。如何做到这一点的一个小例子(jsfiddle上的工作示例)
<ul>
    <li><input type="text" name="textbox[]" /></li>
</ul>
<input type="button" id="addTextbox" value="Add textbox" />
<script type="text/javascript">
$(function(){
    $('#addTextbox').click(function(){
        var li = $('ul li:first').clone().appendTo($('ul'));
        // empty the value if something is already filled in the cloned copy
        li.children('input').val('');
        li.append($('<button />').click(function(){
            li.remove();
            // don't need to check how many there are, since it will be less than 5.
            $('#addTextbox').attr('disabled',false);
        }).text('Remove'));
        // disable button if its the 5th that was added
        if ($('ul').children().length==5){
            $(this).attr('disabled',true);
        }
    }); 
});
</script>

对于服务器端部分,您可以通过$_POST['textbox']

执行foreach()循环

只要给每个文本框一个像"my_input[]"这样的名称,那么在提交表单时,PHP就可以得到作为数组的答案。

$_REQUEST['my_input'];将是存储在每个文本框中的值的数组。

来源:使用jQuery 添加和删除项目

<a href="#" id="add">Add</a>  
<a href="#" id="remove">Remove</a>  
<p><input type="text" value="1" /></p>  
<script type="text/javascript">  
$(function() { // when document has loaded  
    var i = $('input').size() + 1; // check how many input exists on the document and add 1 for the add command to work  
    $('a#add').click(function() { // when you click the add link  
        $('<p><input type="text" value="' + i + '" /></p>').appendTo('body'); // append (add) a new input to the document.  
// if you have the input inside a form, change body to form in the appendTo  
        i++; //after the click i will be i = 3 if you click again i will be i = 4  
    });  
    $('a#remove').click(function() { // similar to the previous, when you click remove link  
    if(i > 1) { // if you have at least 1 input on the form  
        $('input:last').remove(); //remove the last input  
        i--; //deduct 1 from i so if i = 3, after i--, i will be i = 2  
    }  
    });  
    $('a.reset').click(function() {  
    while(i > 2) { // while you have more than 1 input on the page  
        $('input:last').remove(); // remove inputs  
        i--;  
    }  
    });  
});  
</script>  

您将需要动态创建DOM元素。看看它是如何完成的,例如在这个问题中。注意

document.createElement

比使用jquery的语法(如)更快

$('<div></div>')

使用这种技术,你可以创建像这样的输入

<input name="id1"/>
<input name="id2"/>
<input name="id3"/>
<input name="id4"/>
<input name="id5"/>

在提交表单时,您将在查询字符串中获得所有表单,如

...id1=someval1&id2=someval2&...

这样,您就可以根据需要在服务器端处理这个查询。

<form method="POST" id="myform">
<input />
<a href="#add_textbox" id="add_textbox">Add textbox</a>
<input type="submit" value="Submit" />
</form>
<script type="text/javascript">
$(document).ready(function(){
  $('#add_textbox').click(function(){
    var form=$(this).closest('form');
    var count=form.find('input').length();
    form.append('<div class="removable_textbox"><input /><a href="#delete" class="delete_input">delete</a></div>');
    $('.delete_input').click(function(){
      $(this).find('.removable_textbox').remove();
      return false;
    });
    return false;
  });
  $('#myform').submit(function(){
    var i=1;
    $(this).find('input').each(function(){
      $(this).attr('name','input-'+i);
      i++;
    })
  });
});
</script>
<?php
  if(isset($_POST['input-1'])){
    $input_array=$_POST;
  }
?>

像这样的东西?

我写了一个名为textbox的litte jQuery插件。你可以在这里找到它:http://jsfiddle.net/mkuklis/pQyYy/2/您可以在表单元素上初始化它,如下所示:

$('#form').textbox({
  maxNum: 5, 
  values: ["test1"], 
  name: "textbox", 
  onSubmit: function(data) {
    // do something with form data
  }
});

设置是可选的,它们指示:

  • maxNum-屏幕上呈现的元素的最大数量
  • values-初始值的数组(您可以使用它来传递初始值,例如可能来自服务器)
  • name-输入文本字段的名称
  • onSubmit-单击保存按钮时执行onSubmit回调。传递的数据参数保存序列化的表单数据

这个插件并不完美,但它可能是一个良好的开端。