检测是否已按用户点击所有按钮

时间:2017-07-23 09:19:03

标签: javascript jquery css wordpress visual-composer

我正在创建一个“完成任务以解锁”系统,但我在编码时非常新手,无法使其正常工作。我几乎要花几个星期的时间试图去寻找方法,但除非知道,否则什么都没有。我只有非常基本的。

我将把我需要的一切分开,我真的很赞赏你们的任何帮助。

哦,我正在使用 Wordpress Visual Composer

好吧,我已经有了它(或差不多)的代码,但是我没有正常工作。

我需要的是某个按钮(.Test)仅在完成一些任务后出现。

最初,任务将是“访问此链接以解锁”。所以我会给出一些按钮,每个按钮都会打开一个不同的链接。单击时,该按钮将触发操作,解锁按钮以获取cd-keys /链接。但问题是我需要用户点击它的所有按钮。在我正在使用的代码上,当用户单击一个任务按钮时,会出现.Test。

这是我正在使用的代码:

jQuery代码

<script>
jQuery(document).ready(function(){
 jQuery('.Like').click(function() {     //Will open a Facebook page
    jQuery('.Test').show();             //Show the button for the link generation
});
 jQuery('.Follow').click(function() {   //Will open a Twitter page
    jQuery('.Test').show();             //Show the button for the link generation
});
});
</script>

CSS代码

.Test {
    display: none;
}

.Like,
.Follow {
    cursor: pointer;
}

我试图使用&amp;&amp;运算符,但它不起作用。我需要用户点击所有链接按钮来显示按钮生成器。不只是一个。

感谢您的帮助。

如果您需要更多信息,请问我。

2 个答案:

答案 0 :(得分:0)

一种方法是计算这些特定按钮的点击次数,例如通过将按钮存储在Set中并获取集合大小:

jQuery(function(){
  var clicked=new Set;//a new unique set
  jQuery('.social').click(function() {     // some button clicked
       clicked.add(this);      //add this button to set
        if(clicked.size>=3) alert("wohoo, all buttons clicked");//actually: three different buttons with class social clicked
  });
});


<button class="social">Twitter</button>
<button class="social">Facebook</button>
<button class="social">GitHub</button>

http://jsbin.com/fanuloxori/edit?output

请注意,集合非常新,可以用唯一的数组替换它。

您可以为按钮分配唯一的ID,这样您就可以跟踪点击的ID,然后可以将其存储在数据库或用户设备上:

jQuery(function(){
  var clicked=new Set((localStorage.getItem("clicked")||"").split(","));//a new unique set may restored from storage
  jQuery('.social').click(function() {     // some button clicked
       clicked.add(this.id);      //add this buttons id to set
       localStorage.setItem("clicked",[...clicked].join(","))
        if(clicked.size>=3) alert("wohoo, all buttons clicked");//actually: three different buttons with class social clicked
  });
});


<button class="social" id="twitter">Twitter</button>
<button class="social" id="facebook">Facebook</button>
<button class="social" id="github">GitHub</button>

答案 1 :(得分:0)

@Jonasw谢谢老兄。使用没有id的代码可以正常工作。现在我必须点击所有按钮(我已经为“社交”设置了所有类)以显示hiden内容。但是我不能将它与第二个代码的数据存储一起使用。

<script>
jQuery(document).ready(function(){
   var clicked=new Set;
      jQuery('.social').click(function() {
         clicked.add(this);
            if(clicked.size>=2) jQuery('.Test').show();
   });
});
</script>