简体   繁体   中英

How use mousedown/click events for disabled objects?

Considering the following, how can i enable a disabled element on click?

$("element").click(function(){
    //enable element
});

The best I could come up with is:

$('label').click(
    function(){
        $(this).next(':disabled').removeAttr('disabled');
    });

Since the disabled element itself doesn't, at least in Chrome 8/Ubuntu 10.10, respond to click events.

This does assume, of course, that the label precedes the disabled element, and doesnt', in its current form, check that the label corresponds to the next disabled element.

JS Fiddle demo of the above .


Edited to revise the approach, so clicking on the label affects only the relevant input :

$('label').click(
    function(){
        var relevantInput = $(this).attr('for');
        $('#' + relevantInput)
            .removeAttr('disabled');
    });

JS Fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM