简体   繁体   English

如何在while循环中创建“喜欢”按钮,以便我可以为每个循环更改他的类名?

[英]How to create 'like' button in while loop so i can change his class name for every loop?

I want to create like buttons in my comment box. 我想在评论框中创建类似按钮。 Problem is that is in while loop so any button i click will update all buttons , and its because class name of where the output comes is same for all created buttons. 问题是在while循环中,所以我单击的任何按钮都会更新所有按钮,这是因为所有创建的按钮的输出来源类名称都是相同的。

 while{
             some  code ...

      <a ><i class="glyphicon glyphicon-thumbs-up" name="like" onclick="likeAdd()" id="like" role="button"></i></a>
                    <script>
                    function likeAdd(){
        $.post("ajax/add_like.php?id=<?php echo $id_koment; ?>",function(data){
            if ( data =='success')
            {
                likeGet()
            }
            else
            {
                alert(data);
            }
        });
    }

    function likeGet(){
        $.post("ajax/get_like.php?id=<?php echo $id_koment; ?>",function(data){
            $(".likeCount").text(data);

class of span for js js的span类

        });
    }
      </script>
                    <span class="likeCount"> 0 </span>

class of span 跨度类

    } end of while

Pass the ID around as a parameter, rather than hard-coding it in your functions. 将ID作为参数传递,而不是在函数中对其进行硬编码。 eg in pseudo-code 例如用伪代码

while (... as $id) {
    <i onclick="likeAdd(<?php echo $id ?>);">
}


function likeAdd(id) {
     $.ajax('/script.php?id=' + id, ....);
}

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

相关问题 如何在每个循环中更改请求URL? - How can I change request url by every each loop? 如何将此 while 循环转换为 for 循环? - How can I convert this while loop to a for loop? 如何才能使其单击按钮更改其类名和HTML标记的类名? - How can I make it so that clicking a button changes it's class name and the class name of my HTML tag? 如何将循环中的每个结果附加到他自己的幻灯片jQuery Bootstrap轮播中? - How do i append every result from loop in his own slide jQuery Bootstrap carousel? 我怎样才能循环一些代码,以便它在每次 confirm() 返回 true 时重复? - How can I loop some code so that it repeats every time confirm() returns true? 如何在JavaScript中使用多种条件创建While循环? - How can I create a while loop with many conditions in javascript? 我可以使用从数组中获取的名称更改按钮的样式吗? - can i change the style of my button using his name taken from array? 在循环中使用变量更改类名 - Change class name with variables in loop JavaScript播放/暂停音频按钮,更改类并创建自动循环 - JavaScript play/pause audio button, change class and create auto loop 如何在js循环中找到最后一项,以便我可以为其添加一个类? - How do I find the last item in a js loop so I can add a class to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM