Event on a disabled input

前端 未结 10 2515
攒了一身酷
攒了一身酷 2020-11-21 15:53

Apparently a disabled is not handled by any event

Is there a way to work around this issue ?



        
10条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 16:01

    Disabled elements "eat" clicks in some browsers - they neither respond to them, nor allow them to be captured by event handlers anywhere on either the element or any of its containers.

    IMHO the simplest, cleanest way to "fix" this (if you do in fact need to capture clicks on disabled elements like the OP does) is just to add the following CSS to your page:

    input[disabled] {pointer-events:none}
    

    This will make any clicks on a disabled input fall through to the parent element, where you can capture them normally. (If you have several disabled inputs, you might want to put each into an individual container of its own, if they aren't already laid out that way - an extra or a

    , say - just to make it easy to distinguish which disabled input was clicked).


    The downside is that this trick unfortunately won't works for older browsers that don't support the pointer-events CSS property. (It should work from IE 11, FF v3.6, Chrome v4): caniuse.com/#search=pointer-events

    If you need to support older browsers, you'll need to use one of the other answers!

提交回复
热议问题