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条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方