stop label from toggling the input checkbox

后端 未结 5 1426
生来不讨喜
生来不讨喜 2021-01-17 09:34

I have the following html code. When clicking on the label it toggles the checkbox.

5条回答
  •  耶瑟儿~
    2021-01-17 10:26

    The best solution would be to let label toggle the checkbox as that is intuitive and expected behaviour.

    Second best solution is to make sure your checkbox is not nested inside label and label does not have for attribute. If you have some logic that depends on it, you can put data attributes on elements and use those in your logic.

    
    

    Last resort

    You could prevent the default behaviour of the click event using jQuery:

    $('label[for="startClientFromWebEnabled"]').click(function(e) { 
        e.preventDefault();
    });​
    

    Please see this jsFiddle for an example.

提交回复
热议问题