jQuery点击事件只在移动鼠标后工作

jQuery click event only working after moving the mouse

本文关键字:移动 鼠标 工作 事件 jQuery      更新时间:2023-09-26

我已经接近绝望了,但我似乎找不到任何解决办法。我基本上有一个点击事件的问题,它以某种方式充当拖动事件。这很奇怪,因为,据我所知,它真的不应该。

让我解释一下。我有这些简单的单选按钮:

<div class="fruitButtons">
  <input type="radio" name="radio" id="Apple">
    <label for="Apple">Apple</label>
  <input type="radio" name="radio" id="Banana">
    <label for="Banana">Banana</label>
  <input type="radio" name="radio" id="Citron">
    <label for="Citron">Citron</label>
</div>
<br><b>selected:</b>
<div class="status">none</div>

我使用jQuery UI将这些常规按钮通过$(".fruitButtons").buttonset();转换成好看的按钮

现在我需要重置按钮的可能性,即取消选中所有按钮一次。因为我不想要一个单独的重置按钮,我需要重置发生当你点击一个已经选中的按钮。对我来说,这是处理这些单选按钮最直观的方式。

这是(显然是简化的)它应该工作的方式:

$(function() {
    $(".fruitButtons").buttonset();
    var uncheck = apple = banana = citron = 0;
    $(".fruitButtons label").click(function(){
        var name = $(this).text();
        // see if button was previously checked
        // if yes set var 'uncheck' to 1
        if ((name == "Apple") && (apple)) uncheck = 1;
        if ((name == "Banana") && (banana)) uncheck = 1;
        if ((name == "Citron") && (citron)) uncheck = 1;
        apple = banana = citron = 0;
        if (!uncheck) {
            if (name == "Apple") apple = 1;
            if (name == "Banana") banana = 1;
            if (name == "Citron") citron = 1;
            // display name of currently checked button
            $(".status").text(name);
        }
        else {
            // unchecking the hidden input element
            $(this).prev().prop("checked", false);
            // resetting the buttonset to its initial state
            $(this).parent().buttonset("refresh");
            $(".status").text("none");
            uncheck = 0;
       }
    });
});

查看小提琴:http://jsfiddle.net/6yx0rqjf/6/

但是这里有一个问题:取消检查过程不能正常工作。事实上,只有当你在点击时拖动鼠标时才会起作用,也就是说,当你在按下鼠标按钮时稍微移动一下鼠标。

更新$(".status")部分工作完美每次你点击按钮,但取消检查它只有成功,如果你移动鼠标一点。

这怎么可能?这是臭虫吗?我能够在Firefox 40.0.3和Chrome 45.0.2454.85上重现这种行为。我还发现了一个未解决的问题,可能在某种程度上是相关的:jQuery点击事件只工作后移动鼠标在Chrome

那么,有人能帮我解决这个问题吗?这可能吗?

我认为问题是你把点击处理程序的标签,而不是按钮本身。我把它移到按钮上,并调整了代码,它可以正常工作。

$(function() {
    var uncheck = apple = banana = citron = 0;
    $(".fruitButtons").buttonset();
    $(".fruitButtons :radio").click(function(){
        var name = $(this).next("label").text();
        // see if button was previously checked
        // if yes set var 'uncheck' to 1
        if ((name == "Apple") && (apple)) uncheck = 1;
        if ((name == "Banana") && (banana)) uncheck = 1;
        if ((name == "Citron") && (citron)) uncheck = 1;
        apple = banana = citron = 0;
        if (!uncheck) {
            if (name == "Apple") apple = 1;
            if (name == "Banana") banana = 1;
            if (name == "Citron") citron = 1;
            // display name of currently checked button
            $(".status").text(name);
        }
        else {
            // unchecking the hidden input element
            $(this).prop("checked", false);
            // resetting the buttonset to its initial state
            $(this).button("refresh");
            $(".status").text("none");
            uncheck = 0;
       }
    });
});

小提琴