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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据