简体   繁体   English

从foreach循环内的textarea获取值

[英]Getting a value from textarea that is inside of a foreach loop

So, here is the thing - I have a comment section that is generated withing a foreach loop, all the comments have button clicking which will cause bootstrap modal to open with textarea to enter a reply comment. 所以,这就是问题-我有一个foreach循环生成的注释部分,所有注释都有单击按钮,这将导致引导程序模式与textarea打开,以输入回复注释。 The problem is that I can get a value only from first comment on the page, I tried to get value with JS ike this - var comment = $('#textarea_id').val(); 问题是我只能从页面上的第一个注释中获取值,我试图用JS这样获取值-var var comment = $('#textarea_id').val(); and using only PHP ($_POST) but it works only for the first comment. 并且仅使用PHP($ _POST),但仅适用于第一个注释。 Also it tried to put unique id for every textarea, unique name and so on but it doesn't help either. 此外,它尝试为每个文本区域,唯一名称等添加唯一ID,但也无济于事。 Here is some code for your visual understanding (Please, notice that I'm using smarty engine but I think it's the same logic as in regular PHP foreach loop, so don't mind it): 这是一些您的视觉理解代码(请注意,我使用的是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">&times;</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}

So, the main question is - how to get text from textarea which is in foreach loop (By php or javascript)?I will be really happy to see any advice or suggestion! 因此,主要问题是-如何从位于foreach循环中的textarea中获取文本(通过php或javascript)?我将非常高兴看到任何建议或建议! Thank you very much! 非常感谢你!

You need a shared identifier to relate the button to the matching textarea. 您需要一个共享的标识符,以将按钮与匹配的文本区域相关联。 $row.id seems like a good match. $row.id似乎很$row.id

Disclaimer: untested code, so you might have to finagle it a bit to get it to work properly. 免责声明:未经测试的代码,因此您可能必须稍微摆弄一下代码才能使其正常工作。

{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}

Then in your javascript, you can relate it back: 然后在您的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
});

I'm sure there are more efficient ways to do this, maybe you could use a data attribute on the button so you don't have to string-replace bits off the button's id . 我确信有更有效的方法可以做到这一点,也许您可​​以在按钮上使用data属性,这样就不必用字符串替换按钮id This is just a general idea. 这只是一个总的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM