2010-10-14 105 views
0

親愛的所有,我有我的網頁內的兩個按鈕。我想第一次第二個按鈕無法點擊(不活動),但是我們可以點擊第一個按鈕後點擊它。 當然,第一個按鈕點擊後,它變得無效。一個按鈕激活另一個不活動

這個順序:

button 1 ---> active 
button 2 ---> inactive 

then click "button 1" 
button 1 --->inactive 
button 2 ---> active 

then click "button 2" 
button 1 --->active 
button 2 --->inactive 

怎麼做呢?

回答

2

HTML

<input type="button" id="bt1" value="button 1" /> 
<input type="button" id="bt2" value="button 2" disabled="disabled" /> 

jQuery的

$(function(){ 
     $("#bt1").click(function(){ 
      $(this).attr("disabled", "disabled"); 
      $("#bt2").removeAttr("disabled"); 
     }); 

     // if you want first button to be disabled when second button is clicked 
     $("#bt2").click(function(){ 
      $(this).attr("disabled", "disabled"); 
      $("#bt1").removeAttr("disabled"); 
     }); 
    }); 

See this in action

+0

以前從未想過這個解決方案! :D很好 – Jordy 2012-05-31 08:49:57

0

僞代碼:

function button1() { 
    deactivateButton1(); 
    activateButton2(); 
} 

function button2() { 
    deactivateButton2(); 
    activateButton1(); 
} 
0

合併onclicks下面用正確的起始值按鈕的disabled屬性

$('#button1').click(function(){ 
    $('#button2').attr('disabled',''); 
    $('#button1').attr('disabled','disabled'); 
} 

$('#button2').click(function(){ 
    $('#button1').attr('disabled',''); 
    $('#button2').attr('disabled','disabled'); 
} 
1

如果你永遠只能將有兩個按鈕,你真的可以只是這樣做:

$('button').click(function(){ 
    $(this).attr('disabled', true).siblings('button').attr('disabled', false); 

    return false; 
}); 

參見:http://jsfiddle.net/yijiang/6a2pf/

0

對於不適用略去狀態儘量使用屬性禁用如下:

$('id_button_2').disabled=false; 

然後,當你想激活其他按鈕使用:

$('id_button_1').disabled=true; 

請記住,你必須管理的事件功能(例如這種狀態。 onclick)取決於你想要的東西

相關問題