【问题标题】:Copy input hidden value to other input hidden using JS将输入隐藏值复制到使用 JS 隐藏的其他输入
【发布时间】:2023-03-22 16:10:01
【问题描述】:

我在这个 foreach 上有一些输入隐藏,我需要将这些值复制到另一个隐藏在外部的输入

<?php

$comentarios = $SQL->query("SELECT * from comentarios WHERE anime = '$get_anime' AND respostaId = 0");

foreach($comentarios as $comentario) { ?>
    <button class="replyButton" onclick="copyFormValue()">Responder</button>                        
    <input type="hidden" name="comentarioId" id="comentarioId" value="<?php echo $comentario['id']; ?>">
    <input type="hidden" name="comentarioAvatar" value="<?php echo $comentario['avatar']; ?>">
    <input type="hidden" name="comentarioNome" value="<?php echo $comentario['nome']; ?>">
    <input type="hidden" name="comentarioData" value="<?php echo $comentario['data']; ?>">
    <input type="hidden" name="comentarioTexto" value="<?php echo $comentario['comentario']; ?>">
<?php } ?>


<input type="hidden" name="respostaId" id="respostaId" value="">
            

<script>
    function copyFormValue() {
       formInput1 = document.getElementById("comentarioId");
       form2Input1 =  document.getElementById("respostaId");
       
       form2Input1.value = formInput1.value;
    }
</script>

问题是总是复制第一个 foreach 值,我需要将确切的值引用复制到我单击的按钮..

我不想使用帖子和表单来获取帖子值..

编辑:我把代码更清楚地理解了

【问题讨论】:

  • 请发minimal reproducible example,或格式化您的代码以突出您遇到的问题。
  • 我没有看到为 comentarioId 设置的 ID,这是您在函数中寻找的。​​span>

标签: javascript php input hidden


【解决方案1】:

请试试这个:

                <?php foreach($comentarios as $key => $comentario) { ?>
                    <div class="comentarioBox">
                        <img src="images/avatars/<?php echo $comentario['avatar']; ?>"/>
                        <div class="comentario">
                            <div class="comentarioHeader">
                                <span class="usuario"><?php echo $comentario['nome']; ?></span><span class="data">- <?php echo $comentario['data']; ?></span>
                                <button class="replyButton" onclick="copyFormValue(<?php echo $key;?>)">Responder</button>                        
                                <input type="hidden" name="comentarioId" value="<?php echo $comentario['id']; ?>">
                                <input type="hidden" name="comentarioAvatar" value="<?php echo $comentario['avatar']; ?>">
                                <input type="hidden" name="comentarioNome" value="<?php echo $comentario['nome']; ?>">
                                <input type="hidden" name="comentarioData" value="<?php echo $comentario['data']; ?>">
                                <input type="hidden" name="comentarioTexto" value="<?php echo $comentario['comentario']; ?>">
                            </div>
                            <p><?php echo $comentario['comentario']; ?></p>
                            
                            <?php $respostasCount = $SQL->query("SELECT COUNT(*) from comentarios WHERE respostaId > 0 AND respostaId = ". $comentario['id'] ."")->fetch_row(); 
                            if ($respostasCount['0'] > 0) { ?>
                                <button class="respostas" id="respostasBox-<?php echo $comentario['id']; ?>"><?php echo $respostasCount['0']; ?> Resposta<?php echo (($respostasCount['0'] > 1) ? 's' : ''); ?></button>
                            <?php } ?>      

                            <div class="respostasComentarioBox-<?php echo $comentario['id']; ?>" style="display:none;">
                                <?php foreach($respostasComentario as $resposta) { 
                                    if ($resposta['respostaId'] == $comentario['id']) { ?>
                                        <div class="respostaComentarioBox">
                                            <img src="images/avatars/<?php echo $resposta['avatar']; ?>"/>
                                            <div class="respostaComentario">
                                                <div class="comentarioHeader">
                                                    <span class="usuario"><?php echo $resposta['nome']; ?></span><span class="data">- <?php echo $resposta['data']; ?></span>
                                                </div>
                                                <p><?php echo $resposta['comentario']; ?></p>
                                            </div>
                                        </div>
                                    <?php } ?>
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>

                <input type="hidden" name="respostaId" id="respostaId" value="">

                <script>
                    function copyFormValue(id) {
                       document.getElementById("respostaId").value = document.getElementById("comentarioId")[id].value;
                       
                    }
                </script>

【讨论】:

  • 那么请尝试将 id 替换为固定值 0, 1, 2, 3, ... maulally.. document.getElementById("respostaId").value = document.getElementById("commentarioId") [0].值; document.getElementById("respostaId").value = document.getElementById("commentarioId")[1].value; ...
  • 如果它有效,那么您可以获得解决此问题的提示
猜你喜欢
  • 2013-11-17
  • 2013-08-18
  • 2019-01-06
  • 2013-08-18
  • 2021-06-29
  • 2011-12-06
  • 1970-01-01
  • 2018-03-08
相关资源
最近更新 更多