2014-04-30 127 views
0

任何人都可以幫助我如何啓用基於點擊事件的按鈕。如何啓用基於點擊的按鈕甚至

我需要一個場景,我們有2個按鈕,Button1和Button2(默認情況下禁用)。

一旦點擊Button1,說10秒後我的Button2應該啓用。

我在下面的代碼中做錯了?我點擊Button1後,我的Button2沒有啓用。有誰能夠幫助我。

由於提前, 烏代

<html> 
<Head> 
<Title>Synchronization</Title> 
</head> 
<script> 
function PleaseWait() 
{ 
    document.getElementsByName("SampleButton2").disabled=false; 
} 
</script> 
<body> 
<input type="button" name="SampleButton1" value="Button1" onclick="PleaseWait()"> 
<input type="button" name="SampleButton2" value="Button2" disabled> 
</body> 
</html> 

回答

0

試試這個

document.getElementsByName("SampleButton2").removeAttr('disabled'); 

,或者您可以使用

$('SampleButton2').prop('disabled', false); 
1

您可以用jQuery做到這一點。

在$(document).ready()方法中,定義了一個函數PleaseWait(),您可以在其中編寫代碼以從您的按鈕中刪除禁用的屬性。

然後當任何用戶點擊button1時,觸發一個函數在使用setTimeout()方法延遲一段時間後調用pleaseWait()函數。我用了5毫秒來延遲。您可以增加或減少它。

<!DOCTYPE html> 
<html> 
<head> 
<title>DOM Level 0 Events Example</title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
</head> 

<body> 
<input type="button" name="SampleButton1" value="Button1"> 
<input type="button" name="SampleButton2" value="Button2" disabled> 
<script type="text/javascript"> 
    $(document).ready(function(e){ 

    function PleaseWait(){ 
     $('input[value="Button2"]').removeAttr('disabled'); 
    } 

    $('input[value="Button1"]').click(function(e) { 
     setTimeout(PleaseWait, 5000); 
    }); 

    }); 
</script> 
</body> 
</html> 
+0

我可以看到你的代碼對我來說工作的很好,但我是jQuery的新手,你能否提供你在HTML代碼中的輸入?我的代碼中的某些內容是否可以更正以使其正常工作? – Uday

+0

嗨,你的HTML代碼很好。然而在你的javascript代碼中,你已經使用了getElementsByName方法。在這種情況下,你也應該給出該元素的索引。請在下面檢查您的修改代碼希望你能理解:-) ' ' –

相關問題