douqiao5552 2012-04-16 07:46
浏览 27

如何遍历动态(jQuery)生成的texareas数组并将值发布到同一页面?

I'm generating a number of textareas with jQuery. I want to retrieve the values of all the filled out textareas via an on-page POST method (it's kind of a proof of concept, quick and dirty, I wouldn't do this normally), but when I hit submit, I get the value of only one, whichever is the last textarea value posted. I imagine I can do this with an array and a foreach loop, but am unsure how to do it, given all the additions of the jQuery/on-page factors complications in this endeavor. I also imagine it might have something to do with all of the generated textareas having the same name... anyone?

Here's the code-

    <script type="text/javascript">
        $(document).ready(function() {

            $('.textadder').click(function(){
            $("form").append("<p class='introText2'>Enter More Text</p><textarea rows='5' cols='20' name='textForm' class='formText2'></textarea>");
        });



    });/*document ready*/

</script>

<?php

if (isset($_POST['textForm']))
{
$formTxt = $_POST['textForm'];
        echo $formTxt;
}
?>
</head>

<body>


<div id="wrapper">
    <div id="submittedHolder"></div>
    <div class="formBox">
        <form method="post" action="">
            <p class="introText">Please Enter Some Text</p>
            <textarea rows="5" cols="20" name="textForm" class="formText"></textarea>
            <input type="submit" class="submitter" value="Submit">
        </form>

        <div class="textadder"><p>More Text</p></div>
        <div class="clearer"></div>
    </div><!--formBox-->
</div><!--wrapper-->


</body>

</html>

Thanks!

  • 写回答

2条回答 默认 最新

  • douxihui8270 2012-04-16 07:50
    关注

    Use textForm[] in your textarea name instead of textForm:

    <textarea rows="5" cols="20" name="textForm[]" class="formText"></textarea>
    

    After that all "textForm" textareas would be in $_POST['textForm'] array.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c