使用jquery无法单击鼠标?

时间:2013-09-18 20:35:08

标签: jquery

当我尝试单击按钮上的任何一个选项时,有时我会得到一次单击(按预期),有时我会使用Stackoverflow上的某个人给出的下面的代码获得多次单击(不是我想要的) ...

var $document = $(document);
var clickCount = 0;

var treeLogic = function (event) {
    // unbind the element from the event handler
    $document.off("click", "#file", treeLogic);

    clickCount = clickCount + 1; 
    // for testing the number of clicks on each click of a button. 
    // sometimes it shows one click and sometimes it shows more than one click
    // (not what I want).

    // bind the element back to the event handler
    $document.on("click", "#file", treeLogic);
};

$document.on("click", "#file", treeLogic);

HTML代码

<input type='button' id='button'></input>

可能是我的老鼠坏了?或者上面的逻辑很糟糕,如果有的话,有人可以告诉我如何修复它吗?

1 个答案:

答案 0 :(得分:0)

我删除了一些.on和.off绑定的部分......这个工作

Jsfiddled here

HTML

<input type='button' id='file'></input>

的javascript

var $document = $(document);
var clickCount = 0;
var treeLogic = function (event) {
    clickCount = clickCount + 1;
    console.log(clickCount);
};
$document.on("click", "#file", treeLogic);