gpt4 book ai didi

javascript - 设置创建笔记的次数限制

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:18 25 4
gpt4 key购买 nike

我目前正在为我的网站制作一个便利贴,按下加号按钮将创建一个可编辑的便利贴/便签。我现在已经完成了那部分,但我正在努力处理下一部分,即将便签的数量限制为 4 个。我尝试使用一个全局变量作为计数器,因此当计数器为 3 时,它应该停止创建更多便利贴,但不幸的是,它不起作用。

这是我的可行代码的链接: Sticky note

这是我将便利贴的数量限制为 4 个的徒劳尝试。

$("#create").click(function(){
var count = 0;
if( count < 4){
$("#create").click(function() {
$(this).before("<textarea></textarea>");
});
count++;
}
}

任何人都可以给我一些关于如何将注释限制为 4 的指示吗?我一直在研究这个。

最佳答案

只需将 var count = 0; 移到事件监听器之外并移除内部事件监听器:

var count = 0;                                       // outside the scope of the event listener function bellow so it won't get destroyed/recreated each time the function get called (ie when a clicks happen)
$("#create").click(function() {
if(count < 4) { // we haven't yet exceeded the limits
$(this).before("<textarea></textarea>"); // don't attach another click event listener on #create (we are already in one) so just create the note.
count++;
}
});

关于javascript - 设置创建笔记的次数限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47446009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com