Disable button with jQuery -- IE9 problem
I have a snippet of code that disables upload button until something is selected to be uploaded. I just noticed that it does not work in IE9 Beta. Do I need to iterate it somehow more for IE? Here's my code:
$("input:file").change(function(){
if ($(this).val()) {
$("input:submit").attr("disabled",false);
}
});
UPDATE:
I modified my code to add an alert:
$("input:file").change(function(){
alert("...");
if ($(this).val()) {
$("input:submit").removeAttr("disabled");
}
})开发者_如何学编程;
In FF alert comes on and enables button, in IE alert is not triggered.
ONE MORE UPDATE:
The problem went ahead without code modification. In IE9 there's a new icon for "COMPATIBILITY VIEW" next to web address field. I clicked it to enable and then clicked again to disable and the issue went away. My guess, IE blocked jQuery somehow and cached it. By changing the compatibility settings I might've removed cached settings. WEIRD!
try using $("input:submit").attr("disabled","disabled");
to disable and $("input:submit").removeAttr("disabled")
There are browsers out there - obviously also IE9 - that don't allow you to access the value of a file input filed for security reasons.
This is the code I use to disable or re-enable a control:
function handleControlDisplay(className, foundCount, checked) {
var button = jQuery(className);
if (foundCount > 0 && foundCount == checked) {
// enable
button.removeAttr("disabled");
}
else {
// set the disabled attribute
button.attr("disabled", "true");
};
}
Here is a detailed tutorial from my website showing how to do this: jQuery disable button. In a nutshell the best way to disable a button using jQuery is using the following code:
$('.buttonElement').attr('disabled', 'disabled'); //TO DISABLED
$('.buttonElement').removeAttr('disabled'); //TO ENABLE
Hope this helps someone out.
精彩评论