duansha8115 2012-10-08 00:15
浏览 20
已采纳

修改javascript以输出表单名称而不是表单ID

I made several input field boxes with the following code:

<script type='text/javascript'>//<![CDATA[ 
    $(window).load(function(){
        $('.add').click(function(){
            par_id=$(this).parent().attr('id');
            count=$("#"+par_id+"> input").length;
            id=par_id.toString();
            $(this).parent().append("<br/><input type='text' id='"+id+"'>");
        });
    });//]]>  
</script>

I'm using the aforementioned code in order to create input fields that can dynamically add new fields.

This code creates multiple identical form ids, but I want it to create multiple identical form names so I can output it altogether using the foreach command.

Now I want to output the input data into a PHP using this

<?php
    foreach($_POST['formid'] as $value) {
        echo "$value <br />";
    }
?>

What I want to ask is that the foreach command will only output data from the form name, but the javascript code I am using creates more input forms by the form id, not the form name.

So I need a code that will create more input boxes with form name, not form id!

I'm not sure how to modify the original code, since I got it from another website.

Please help!

  • 写回答

3条回答 默认 最新

  • donglu2008 2012-10-08 02:03
    关注

    You simply need to add a name attribute to the code which is inserted by the javascript function.

    Original:

    $(this).parent().append("<br/><input type='text' id='"+id+"'>");
    

    Ammended:

    $(this).parent().append("<br/><input type='text' name='yourinputname' id='"+id+"'>");
    

    EDIT: Just and afterthought...

    If the ID serves no purpose and only the name attribute is required, the majority of this code is unnecessary and can be reduced to the following.

    <script type='text/javascript'>//<![CDATA[ 
        $(window).load(function(){
            $('.add').click(function(){
                $(this).parent().append("<br/><input type='text' name='yourinputname');
            });
        });//]]>  
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭