[英]Getting a value from textarea that is inside of a foreach loop
所以,这就是问题-我有一个foreach
循环生成的注释部分,所有注释都有单击按钮,这将导致引导程序模式与textarea打开,以输入回复注释。 问题是我只能从页面上的第一个注释中获取值,我试图用JS这样获取值-var 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)?我将非常高兴看到任何建议或建议! 非常感谢你!
您需要一个共享的标识符,以将按钮与匹配的文本区域相关联。 $row.id
似乎很$row.id
。
免责声明:未经测试的代码,因此您可能必须稍微摆弄一下代码才能使其正常工作。
{foreach from=$comments item=row}
...
<textarea class="form-control" id="textarea_{$row.id}" name="" rows="8" maxlength="5550"></textarea>
...
<button id="submit_post_{$row.id}" class="reply btn btn-success" name="submit" type="submit">Post</button>
...
{/foreach}
然后在您的JavaScript中,可以将其关联回去:
$('button.reply').click(function() {
var button_id = $(this).prop('id');
var row_id = button_id.replace('submit_post_', '');
var textarea_id = '#textarea_' + row_id;
var comment = $(textarea_id).val();
// now send {comment} to the server using ajax
});
我确信有更有效的方法可以做到这一点,也许您可以在按钮上使用data
属性,这样就不必用字符串替换按钮id
。 这只是一个总的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.