最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Preventing double-click bug with checkbox + label combination - Stack Overflow

programmeradmin4浏览0评论

Note this issue may not apply to the general public, as it does not occur unless you're a fast clicker. (150-200ms/click) The reason I'm posting this issue is because my application has a form with 20+ checkboxes next to each other, and after extensive research I've found no related questions on this matter.

Here's a simplified scenario - 4 checkboxes and 4 labels, one for each checkbox id:

[CB1] Label 1
[CB2] Label 2
[CB3] Label 3
[CB4] Label 4

Assume in each case all CBs are unchecked.

Expected Behavior:

  1. I click on the 4 CBs in rapid succession, they will all become checked. (true)
  2. I click on the 4 Labels in rapid succession, and the corresponding CBs become checked. (only true for Chrome, but still not optimal)

Actual Behavior for case 2 on Win 7 (clicking on labels, because as you know, labels are big and style-able, and the checkboxes are tiny and OS-dependent):

  1. (In Firefox 19) CB2 and CB4 are left unchecked, and while going down the list the word "Label" gets highlighted for Label 2 and Label 4, as if I double-clicked on them.
  2. (In Chrome 26) All CBs get correctly checked, but while going down the list the word "Label" gets highlighted for Label 2 and Label 4, as if I double-clicked on them.
  3. (In IE 10) CB2 and CB4 are left unchecked, but no false highlighting.

The erroneous behavior could be justified if the clicks are on the same element. In our case those are clearly unique checkboxes with different IDs and Names. So the results are wildly unexpected.

So my question is:

Is there a way to disable firing the double click event when I rapidly click on the different checkboxes, but yet still check them with fast single clicks?

The closest I've come to is the following script, which interestingly made Firefox behave like Chrome, and Chrome behave like Firefox:

jQuery(document).on('dblclick', 'input:checkbox+label', function(event){
    console.log('ugly hack fired');
    $(this).click();
    event.preventDefault();
})

Note this issue may not apply to the general public, as it does not occur unless you're a fast clicker. (150-200ms/click) The reason I'm posting this issue is because my application has a form with 20+ checkboxes next to each other, and after extensive research I've found no related questions on this matter.

Here's a simplified scenario - 4 checkboxes and 4 labels, one for each checkbox id:

[CB1] Label 1
[CB2] Label 2
[CB3] Label 3
[CB4] Label 4

Assume in each case all CBs are unchecked.

Expected Behavior:

  1. I click on the 4 CBs in rapid succession, they will all become checked. (true)
  2. I click on the 4 Labels in rapid succession, and the corresponding CBs become checked. (only true for Chrome, but still not optimal)

Actual Behavior for case 2 on Win 7 (clicking on labels, because as you know, labels are big and style-able, and the checkboxes are tiny and OS-dependent):

  1. (In Firefox 19) CB2 and CB4 are left unchecked, and while going down the list the word "Label" gets highlighted for Label 2 and Label 4, as if I double-clicked on them.
  2. (In Chrome 26) All CBs get correctly checked, but while going down the list the word "Label" gets highlighted for Label 2 and Label 4, as if I double-clicked on them.
  3. (In IE 10) CB2 and CB4 are left unchecked, but no false highlighting.

The erroneous behavior could be justified if the clicks are on the same element. In our case those are clearly unique checkboxes with different IDs and Names. So the results are wildly unexpected.

So my question is:

Is there a way to disable firing the double click event when I rapidly click on the different checkboxes, but yet still check them with fast single clicks?

The closest I've come to is the following script, which interestingly made Firefox behave like Chrome, and Chrome behave like Firefox:

jQuery(document).on('dblclick', 'input:checkbox+label', function(event){
    console.log('ugly hack fired');
    $(this).click();
    event.preventDefault();
})
Share Improve this question edited Apr 6, 2013 at 10:55 Scott Yang asked Apr 6, 2013 at 10:35 Scott YangScott Yang 2,4282 gold badges19 silver badges21 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 10

Finally got one very ugly hack that worked for all the browsers, hopefully this will help anyone else who comes across the problem:

Disable selection with css because doing it in JS is simply too inefficient:

.form_class input[type=checkbox] + label{
    -webkit-user-select:none;
    -khtml-user-select:none;
    -moz-user-select:none;
    -o-user-select:none;
    user-select:none;
}

Prevent ALL clicking in JS, and manually do what clicking should do:

jQuery(document).on('click', '.form_class input:checkbox+label', function(event){
    // Assuming label comes after checkbox
    $(this).prev('input').prop("checked", function(i, val){
        return !val;
    });
    event.preventDefault();
})

This would do it-

$("input[type='checkbox']").dblclick(function (event)
{
    event.preventDefault();
});

Try this:

$(document).on('dblclick', 'input:checkbox, label', function (event) {
    event.preventDefault();
    // Your code goes here   
})

OR

$("input:checkbox, label").dblclick(function (event) {
    event.preventDefault();
    // Your code goes here
});

OR

$('input:checkbox').add('label').dblclick(function (event) {
    event.preventDefault();
    // Your code goes here
});

I was also facing a similar issue which had me spend whole night on how this can be fixed for checkbox. I was also listening to the 'dblclick' event to prevent any action from happening on double click on a checkbox. ex:

#(".some_class").on("dblclick",function(event){
event.preventDefault();
});

But the problem here was that it was firing the event after the action was done. So all the damage was already done.

There is a very simple way to tackle this problem that is by listening for the event 'change' instead of listening to 'click'. In this was we are triggering the event when there is a state change from check to unchecked or from unchecked to checked and not on click or double click.

#(".some_class").on("change",function(event){
event.preventDefault();
});
$('.checkbox_class').click(function(event){
    if (event.ctrlKey){ event.preventDefault();
//rest of the code

seems to work

发布评论

评论列表(0)

  1. 暂无评论