2011-02-07 79 views
1

我有一段代碼禁用上傳按鈕,直到選擇要上傳的東西。我只注意到它在IE9 Beta中不起作用。我需要爲IE更多地迭代它嗎?這裏是我的代碼:使用jQuery禁用按鈕 - IE9問題

$("input:file").change(function(){ 
    if ($(this).val()) { 
     $("input:submit").attr("disabled",false); 
    } 
}); 

UPDATE:

我修改了代碼,添加提醒:

$("input:file").change(function(){ 
    alert("..."); 
    if ($(this).val()) { 
     $("input:submit").removeAttr("disabled"); 
    } 
}); 

在FF警報亮起並啓用按鈕,在IE警報不會被觸發。

更多更新:

問題繼續進行,無需修改代碼。在IE9中,網址字段旁邊有一個新的「兼容性視圖」圖標。我點擊它啓用,然後再次點擊禁用,問題就消失了。我的猜測是,IE以某種方式阻止了jQuery並緩存了它。通過更改兼容性設置,我可能會刪除緩存的設置。奇怪的!

+0

你確切地知道哪個函數調用沒有做它應該做的?例如,「change」事件是否永遠不會觸發? '$(this).val()`不會返回真值嗎?等 – 2011-02-07 16:36:35

+0

當我選擇文件時,該按鈕保持禁用。 – santa 2011-02-07 16:37:49

+1

$(「input:submit」)。removeAttr(「disabled」)應該啓用該元素 – 2011-02-07 16:48:32

回答

3

嘗試使用$("input:submit").attr("disabled","disabled");禁用和$("input:submit").removeAttr("disabled")

0

有瀏覽器在那裏 - 顯然也是IE9 - 不允許您訪問申請出於安全原因,文件輸入的值。

0

這是我使用禁用或重新啓用控件的代碼:

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"); 

    }; 

} 
0

這是從我的網站展示瞭如何做到這一點的詳細教程:jQuery disable button。簡而言之,使用jQuery禁用按鈕的最佳方法是使用以下代碼:

$('.buttonElement').attr('disabled', 'disabled'); //TO DISABLED 
$('.buttonElement').removeAttr('disabled'); //TO ENABLE 

希望這可以幫助別人。