Prevent checkbox from ticking/checking COMPLETELY

前端 未结 13 535
醉梦人生
醉梦人生 2020-12-09 07:48

I have been asked to disable the \"ticking\" of a checkbox. I am not being asked to disable the checkbox, but to simply disable the \"ticking\".

In other w

相关标签:
13条回答
  • 2020-12-09 08:06

    It is very important to use return false at the end.

    Something like this:

    $("#checkbox").click((e) => {
      e.stopPropagation();
      return false;
    });
    
    0 讨论(0)
  • 2020-12-09 08:07

    This effect can't be suppressed I fear. As soon as you click on the checkbox, the state (and rendering) is changed. Then the event handlers will be called. If you do a event.preventDefault(), the checkbox will be reset after all the handlers are executed. If your handler has a long execution time (easily testable with a modal alert()) and/or the rendering engine repaints before reseting, the box will flicker.

    $('input[type="checkbox"]').click(function(event) {
        this.checked = false; // reset first
        event.preventDefault();
        // event.stopPropagation() like in Zoltan's answer would also spare some
        // handler execution time, but is no more needed here
    
        // then do the heavy processing:
        alert('Break');
    });
    

    This solution will reduce the flickering to a minimum, but can't hinder it really. See Thr4wn's and RobG's answer for how to simulate a checkbox. I would prefer the following:

    <button id="settings" title="open extended settings">
        <img src="default_checkbox.png" />
    </button>
    
    document.getElementById("settings").addEventListener("click", function(e) {
        var img = this.getElementsByTagName("img")[0]);
        openExtendedSettingsDialog(function callbackTick() {
            img.src = "checked_checkbox.png";
        }, function callbackUntick() {
            img.src = "unchecked_checkbox.png";
        });
    }, false);
    
    0 讨论(0)
  • 2020-12-09 08:13

    Isn't is simpler ? :

    <input type="checkbox" onchange="this.checked = !this.checked">
    
    0 讨论(0)
  • 2020-12-09 08:18

    Sounds to me like you are using the wrong interface element, a more suitable one would be a button that is disabled by default, but enabled when that option is available. The image displayed can be whatever you want.

    <button disabled onclick="doSomething();">Some option</button>
    

    When users have selected that feature, enable the button. The image on the button can be modified by CSS depending on whether it's enabled or not, or by the enable/disable function.

    e.g.

    <script type="text/javascript">
    
    function setOption(el) {
      var idMap = {option1:'b0', option2: 'b1'};
      document.getElementById(idMap[el.value]).disabled = !el.checked; 
    }
    
    </script>
    
    <div><p>Select options</p>
      <input type="checkbox" onclick="setOption(this);" value="option1"> Option 1
      <br>
      <input type="checkbox" onclick="setOption(this);" value="option2"> Option 2
      <br>
    </div>
    <div>
      <button id="b0" onclick="alert('Select…');" disabled>Option 1 settings</button>
      <button id="b1" onclick="alert('Select…');" disabled>Option 2 settings</button>
    </div>
    
    0 讨论(0)
  • 2020-12-09 08:19

    The Event.preventDefault method should work for change, keydown, and mousedown events, but doesn't in my testing.

    My solution to this problem in a Mozilla Firefox 53.0 extension was to toggle an HTML class that enabled/disabled the CSS declaration pointer-events: none being applied to the checkbox. This addresses the cursor-based case, but not the key-based case. See https://www.w3.org/TR/SVG2/interact.html#PointerEventsProp.

    I addressed the key-based case by adding/removing an HTML tabindex="-1" attribute. See https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex.

    Note that disabling pointer-events will disable your ability to trigger CSS cursors on hover (e.g., cursor: not-allowed). My checkbox was already wrapped in a span element, so I added an HTML class to that span element which I then retargeted my CSS cursor declaration onto.

    Also note that adding a tabindex="-1" attribute will not remove focus from the checkbox, so one will need to explicitly defocus it by using the HTMLElement.blur() method or by focusing another element to prevent key-based input if the checkbox is the active element at the time the attribute is added. Whether or not the checkbox is the focused element can be tested with my_checkbox.isEqualNode(document.activeElement).

    0 讨论(0)
  • 2020-12-09 08:21

    With CSS, you can change the image of the checkbox. See http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/ and also CSS Styling Checkboxes .

    I would disable the checkbox, but replace it with an image of a working checkbox. That way the checkbox doesn't look disabled, but won't be clickable.

    0 讨论(0)
提交回复
热议问题