【问题标题】:dynamic form add/remove field动态表单添加/删除字段
【发布时间】:2019-04-19 09:28:04
【问题描述】:

我尝试创建一个可以动态添加/删除输入的表单。 我可以通过单击一个按钮来创建一个新行,但是当我将一个值传递给另一个页面时,我的输入似乎没有被收集。

这是代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(e){
            //var               
            var x = 1;

            //add
            $("#add").click(function(e){
                x++;
                var mark = '<tr><td width="200" height="30" align="center">'+x+'</td><td width="200" height="30" align="center"><input type="text" name="item_name[]" /></td><td width="200" height="30" align="center"><input type="text" name="item_fact[]" /></td><td width="200" height="30" align="center"><input type="text" name="item_desc[]" /></td><td width="200" height="30" align="center"><input type="text" name="item_amount[]"/></td><td width="200" height="30" align="center"><input type="text" name="item_price[]" /></td><td width="200" height="30" align="center"><a herf="#" id="remove" class="button">X</a></td></tr>';
                $("#dynamic_field").append(mark);
            });
            //remove
            $("#dynamic_field").on('click','#remove', function(e){
                $(this).closest('tr').remove();
                x--;
            });
        });
    </script>

这是html代码

<tr id="container">
    <td width="200" height="30" align="center">1</td>
    <td width="200" height="30" align="center"><input type="text" name="item_name[]"/></td>
    <td width="200" height="30" align="center"><input type="text" name="item_fact[]"/></td>
    <td width="200" height="30" align="center"><input type="text" name="item_desc[]"/></td>
    <td width="200" height="30" align="center"><input type="text" name="item_amount[]"/></td>
    <td width="200" height="30" align="center"><input type="text" name="item_price[]"/></td>
    <td width="200" height="30" align="center"><a herf="#" id="add" class="button">Add more</a></td>
</tr>

print_r 的值它传递输入只是html 输入。

item_name => Array ( [0] =>test 1)

【问题讨论】:

  • 如何将值传递给其他页面?
  • 我假设您正在执行 ajax 调用以将值传递给“其他页面”。您还必须为 ajax 添加代码。
  • 我通过 sumbit 形式通过 post 方法传递值。我需要编码以从 ajax 传递值?我想只需附加相同的输入名称即可。有什么样品可以看吗?

标签: php jquery forms dynamic


【解决方案1】:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(e){

            var x = 1; //for row count

            //add more button click 
            $("#add").click(function(e){
                 x++;   

//添加更多点击的行html
var 标记 = ''+x+'X';

             $("#mt tr:last").after(mark); //add the row after the last existing tr 

            });
            //code for X click
            $("#mt").on('click','#remove', function(e){
               //remove the current row
                $(this).closest('tr').remove();
                x--;
            });




        });
    </script>


 <table id="mt">
 <tr id="container">

                <td width="200" height="30" align="center">1</td>
                <td width="200" height="30" align="center"><input type="text" name="item_name[]"/></td>
                <td width="200" height="30" align="center"><input type="text" name="item_fact[]"/></td>
                <td width="200" height="30" align="center"><input type="text" name="item_desc[]"/></td>
                <td width="200" height="30" align="center"><input type="text" name="item_amount[]"/></td>
                <td width="200" height="30" align="center"><input type="text" name="item_price[]"/></td>
                <td width="200" height="30" align="center"><a href="javascript:void(0)" id="add" class="button">Add more</a></td>

    </tr>

</table>

【讨论】:

  • 当用户点击添加更多按钮 $("#mt tr:last").after(mark);表示在最后一个现有表行(tr)之后添加一行。 $("#mt").on('click','#remove', function(e){ $(this).closest('tr').remove(); x--; });删除当前点击的行。
  • 如有任何疑问请告诉我。
  • 请在代码中添加这样的解释,而不是在评论部分
猜你喜欢
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
相关资源
最近更新 更多