Event on a disabled input

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

Apparently a disabled is not handled by any event

Is there a way to work around this issue ?



        
相关标签:
10条回答
  • 2020-11-21 16:03

    suggestion here looks like a good candidate for this question as well

    Performing click event on a disabled element? Javascript jQuery

    jQuery('input#submit').click(function(e) {
        if ( something ) {        
            return false;
        } 
    });
    
    0 讨论(0)
  • 2020-11-21 16:05

    We had today a problem like this, but we didn't wanted to change the HTML. So we used mouseenter event to achieve that

    var doThingsOnClick = function() {
        // your click function here
    };
    
    $(document).on({
        'mouseenter': function () {
            $(this).removeAttr('disabled').bind('click', doThingsOnClick);
        },
        'mouseleave': function () {
            $(this).unbind('click', doThingsOnClick).attr('disabled', 'disabled');
        },
    }, 'input.disabled');
    
    0 讨论(0)
  • 2020-11-21 16:12

    Instead of disabled, you could consider using readonly. With some extra CSS you can style the input so it looks like an disabled field.

    There is actually another problem. The event change only triggers when the element looses focus, which is not logic considering an disabled field. Probably you are pushing data into this field from another call. To make this work you can use the event 'bind'.

    $('form').bind('change', 'input', function () {
        console.log('Do your thing.');
    });
    
    0 讨论(0)
  • 2020-11-21 16:12

    I find another solution:

    <input type="text" class="disabled" name="test" value="test" />
    

    Class "disabled" immitate disabled element by opacity:

    <style type="text/css">
        input.disabled {
            opacity: 0.5;
        }
    </style>
    

    And then cancel the event if element is disabled and remove class:

    $(document).on('click','input.disabled',function(event) {
        event.preventDefault();
        $(this).removeClass('disabled');
    });
    
    0 讨论(0)
提交回复
热议问题