简体   繁体   中英

Disable/Enable input:date on checkbox click doen't work properly?

I'm trying to disable the Date field on uncheck checkbox and vice-versa.

http://jsfiddle.net/MyNameIsSakthi/ZKw9x/2/

Also this works differently in different browsers!

$('#certExpiry').click(function(){
if($('#certExpiry').is(':checked')){
    alert('checked');
    $('#expiryDate').removeProp('disabled');
}
else
    alert('uncheck');
    $('#expiryDate').prop('disabled',true);
});

You probably want something like this...

$('#certExpiry').click(function () {
    $('#expiryDate').prop('disabled', !this.checked);
});

jsFiddle .

Also, don't use removeProp() like that. The documentation makes this clear...

Note: Do not use this method to remove native properties such as checked , disabled , or selected . This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.

You forgot to put your else code in brackets {}

http://jsfiddle.net/ZKw9x/4/

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