x 次后禁用点击

发布于 2024-12-27 20:40:47 字数 57 浏览 1 评论 0原文

假设用户可以单击按钮 (div) 7 次。单击 7 次后,该按钮必须消失或变得不可单击。我该怎么做?

Let's say the user is able to click a button (div) 7 times. After clicking it 7 times, the button must disappear or become non clickable. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

帅气尐潴 2025-01-03 20:40:47

尽管您需要自己充实一些细节,但这应该可行。按钮:

<div class="button">button</div>

JS:

<script>
$('div.button').click(function() {
  var tally = ($(this).data('clicks') || 0) + 1;
  if ( tally < 7 ) {
    $(this).data('clicks', tally);
    console.log(tally);
  }
});
</script>

我只是停止处理点击,但是您当然可以隐藏该按钮,而不需要太多的努力。

This should work although you'll need to flesh out some details yourself. The button:

<div class="button">button</div>

The JS:

<script>
$('div.button').click(function() {
  var tally = ($(this).data('clicks') || 0) + 1;
  if ( tally < 7 ) {
    $(this).data('clicks', tally);
    console.log(tally);
  }
});
</script>

I've simply stopped processing the click, but you can hide the button of course without too much effort.

残龙傲雪 2025-01-03 20:40:47

像这样的东西应该有效:

$('button').data('count', 0).on('click', function (e) {
    var $t = $(this),
        count = ++($t.data('count'));

    // kill it with fire
    if (count === 7) {
        $t.remove();
    }

    // increment the count
    $t.data('count',count);

});

这样,您可以避免全局变量并使计数与其所属的特定元素相关联。 (想象一下,如果您有数百个这样的按钮!)

Something like this should work:

$('button').data('count', 0).on('click', function (e) {
    var $t = $(this),
        count = ++($t.data('count'));

    // kill it with fire
    if (count === 7) {
        $t.remove();
    }

    // increment the count
    $t.data('count',count);

});

This way, you avoid global variables and keep your count associated to the particular elements it pertains to. (Just imagine if you have hundreds of these buttons!)

尽揽少女心 2025-01-03 20:40:47

至少有两种方法可以做到这一点...

使用闭包

只需在事件处理程序外部定义变量并在每次单击时增加它。然后检查它有什么价值。它可能如下所示:

// method 1 - using closure
(function(){
    var click_counter = 0;
    jQuery('#link1').on('click', function(event){
        event.preventDefault();
        var el = jQuery(this);
        click_counter += 1;
        if (!el.hasClass('inactive')){
            // should be activated
            alert('Clicked the link ' + click_counter + ' time(s)');
        };
        if (click_counter >= 7){
            // deactivate
            el.addClass('inactive');
        };
    });
})();

使用 jQuery.data() 将点击计数器关联到元素

您还可以使用 jQuery 的功能之一将数据附加到元素。它可能看起来像这样:

// method 2 - using jQuery.data()
jQuery('#link2').on('click', function(event){
    event.preventDefault();
    var el = jQuery(this);
    var click_counter = (el.data('click-counter') || 0) + 1;
    console.log(click_counter);
    el.data('click-counter', click_counter);
    if (!el.hasClass('inactive')){
        // should be activated
        alert('Clicked the link ' + click_counter + ' time(s)');
    };
    if (click_counter >= 7){
        // deactivate
        el.addClass('inactive');
    };
});

实例

您可以看到两个代码都正常工作 - 只需点击此链接即可观看演示: http:// jsfiddle.net/3wt8h/

jQuery 免责声明 < 1.7

如果您的 jQuery 版本早于 1.7,您将无法使用 .on() 函数。只需使用 .bind() 即可,它的工作原理应该完全相同。

There are at least two methods of doing this...

Using closure

Just define variable outside the event handler and increase it with every click. Then just check what value it has. It could look like this:

// method 1 - using closure
(function(){
    var click_counter = 0;
    jQuery('#link1').on('click', function(event){
        event.preventDefault();
        var el = jQuery(this);
        click_counter += 1;
        if (!el.hasClass('inactive')){
            // should be activated
            alert('Clicked the link ' + click_counter + ' time(s)');
        };
        if (click_counter >= 7){
            // deactivate
            el.addClass('inactive');
        };
    });
})();

Associating click counter to the element using jQuery.data()

You can also use one of the jQuery's features for attaching data to the elements. It could look like this:

// method 2 - using jQuery.data()
jQuery('#link2').on('click', function(event){
    event.preventDefault();
    var el = jQuery(this);
    var click_counter = (el.data('click-counter') || 0) + 1;
    console.log(click_counter);
    el.data('click-counter', click_counter);
    if (!el.hasClass('inactive')){
        // should be activated
        alert('Clicked the link ' + click_counter + ' time(s)');
    };
    if (click_counter >= 7){
        // deactivate
        el.addClass('inactive');
    };
});

Live examples

You can see both codes work correctly - just follow this link to the demo: http://jsfiddle.net/3wt8h/

Disclaimer for jQuery < 1.7

If your version of jQuery is older than 1.7, you will not have .on() function available. Just use .bind() instead, it should work exactly the same.

谎言 2025-01-03 20:40:47
  1. 向 div 添加一个 click 处理程序
  2. 每次用户单击都会增加全局计数时,
  3. 。当 count === 7 时,将 visible css 类设置为隐藏。
  1. add a click handler to the div
  2. each time the user clicks increment a global count.
  3. when the count === 7, set the visible css class to hidden.
别把无礼当个性 2025-01-03 20:40:47

简短的答案是增加一个变量作为点击操作的一部分以跟踪按钮被点击的次数。

您应该避免将此变量设置为全局变量。您可以通过在函数范围内声明该变量来实现此目的,并让该函数设置点击行为。

使用 jQuery:

function initClick () {

    var clickCounter = 0;

    jQuery('#myLink').click(function (e) {
        if (clickCounter >= 7) {
            return;
        }
        // do stuff
        clickCounter++;
    });

}

initClick();

The short answer is to increment a variable as part of the click action to keep track of the number of times the button has been clicked.

You should avoid having this variable be global. You can do this by declaring the variable within the scope of a function, and have that function set up the click behavior.

With jQuery:

function initClick () {

    var clickCounter = 0;

    jQuery('#myLink').click(function (e) {
        if (clickCounter >= 7) {
            return;
        }
        // do stuff
        clickCounter++;
    });

}

initClick();
久隐师 2025-01-03 20:40:47
$(function(){
    $('#myDiv').data('clickCount',0).click(function(){
        $this = $(this);
        var clickCount = Number($this.data('clickCount')) + 1;
        if(clickCount <= 7){
            //Perform click action here
            $this.data('clickCount',clickCount);
        }
        if(clickCount == 7){
            $this.hide();//for example
        }
    });
});
$(function(){
    $('#myDiv').data('clickCount',0).click(function(){
        $this = $(this);
        var clickCount = Number($this.data('clickCount')) + 1;
        if(clickCount <= 7){
            //Perform click action here
            $this.data('clickCount',clickCount);
        }
        if(clickCount == 7){
            $this.hide();//for example
        }
    });
});
沫雨熙 2025-01-03 20:40:47
$("div.button").click(function(){
   var count = parseInt($(this).data("clicks_count"));
   if(isNaN(count))
      $(this).data("clicks_count","0");
   else if(count>=6)
      $(this).hide();
   else
      $(this).data("clicks_count",count+1);
});

当然,在你的 html 中添加带有相关类的按钮,
IE:

  <input type="button" class="button" value="Click Me" />
$("div.button").click(function(){
   var count = parseInt($(this).data("clicks_count"));
   if(isNaN(count))
      $(this).data("clicks_count","0");
   else if(count>=6)
      $(this).hide();
   else
      $(this).data("clicks_count",count+1);
});

and off course in your html add the button with the relevant class,
i.e.:

  <input type="button" class="button" value="Click Me" />
倾城花音 2025-01-03 20:40:47

这就是我要做的

http://jsfiddle.net/einar/E9Wkr/

var countclick = 0;

$(".button").on("click", function() {
    countclick = countclick + 1;
    if (countclick >= 7) {$(".button").off("click"); } 
});

This is what I would do

http://jsfiddle.net/einar/E9Wkr/

var countclick = 0;

$(".button").on("click", function() {
    countclick = countclick + 1;
    if (countclick >= 7) {$(".button").off("click"); } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文