开发者

alerting the user when clicking a disabled radiobutton

开发者 https://www.devze.com 2023-03-09 12:21 出处:网络
I have a list of radio buttons that is initially disabled, and I if the user clicks or tries to select one, he gets an alert telling him that he must be logged in to select an option.

I have a list of radio buttons that is initially disabled, and I if the user clicks or tries to select one, he gets an alert telling him that he must be logged in to select an option.

开发者_运维知识库

How to do it with jquery?


Apparently, disabled elements don't fire mouse events: Event on a disabled input


This might be helpful: http://blog.pengoworks.com/index.cfm/2010/4/23/Attaching-mouse-events-to-a-disabled-input-element

I customized code from the page to be more useful in this case ( http://jsfiddle.net/jqzWK/ ):

var disabled = $('input[type="radio"]:disabled');
disabled.each(function () {
    var self = $(this),
        field = self,
        overlay = $('<div />');
    if (self.parent().is('label')) {
        field = self.parent();
    }
    overlay.css({
        position: 'absolute',
        top: field.position().top,
        left: field.position().left,
        width: field.outerWidth(),
        height: field.outerHeight(),
        zIndex: 10000,
        backgroundColor: '#ffffff',
        opacity: 0
    }).click(function () {
        alert('You must log in.');
    });
    self.parent().append(overlay);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消