【发布时间】:2017-06-15 20:53:02
【问题描述】:
所以,事情是这样的——我有一个使用foreach 循环生成的评论部分,所有 cmets 都有按钮单击,这将导致引导模式打开并使用 textarea 输入回复评论。
问题是我只能从页面上的第一条评论中获取价值,我尝试使用 JS 来获取价值 - var comment = $('#textarea_id').val(); 并且仅使用 PHP ($_POST) 但它仅适用于第一条评论。它还尝试为每个文本区域、唯一名称等设置唯一 ID,但它也无济于事。
这是一些用于视觉理解的代码(请注意,我使用的是 smarty 引擎,但我认为它与常规 PHP foreach 循环中的逻辑相同,所以不要介意):
{foreach from=$comments item=row}
Here is the body of comment and button to trigger reply modal
And this is reply modal with textarea in it:
<!-- Reply Modal -->
<div class="modal fade" id="{$row.id}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content" style="word-wrap:break-word;">
<div class="modal-header love-modal">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title" id="myModalLabel">Reply to <a href="{$abslink}profile/{$row.username}">{$row.username}</a></h3>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" action="#" method="post">
<textarea class="form-control" id="someidfortextarea" name="" rows="8" maxlength="5550"></textarea>
</form>
</div>
<div class="modal-footer">
<button class="reply btn btn-success" name="submit" type="submit">Post</button>
<button type="button" class="btn btn-success" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
{/foreach}
所以,主要问题是 - 如何从 foreach 循环中的 textarea 获取文本(通过 php 或 javascript)?我很高兴看到任何建议或建议!非常感谢你!
【问题讨论】:
-
你也可以发布你的javascript吗?
-
现在我只是出于调试目的将值记录到浏览器的控制台,但即使是关于我的 JS 代码,它也可能与我尝试获取值的方式有关。
$(function() { $(".reply").click(function() { var comment = $('#comment').val(); var topic_id = $('#topic_id').val(); // get the page id from the hidden topic_id field var dataFields = {'comment': comment, 'topic_id': topic_id}; // prepare datas string console.log(comment); console.log(dataFields); }); });
标签: javascript php jquery html foreach