一个按钮激活另一个不活动
问题描述:
亲爱的所有,我有我的网页内的两个按钮。我想第一次第二个按钮无法点击(不活动),但是我们可以点击第一个按钮后点击它。 当然,第一个按钮点击后,它变得无效。一个按钮激活另一个不活动
这个顺序:
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
怎么做呢?
答
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");
});
});
答
伪代码:
function button1() {
deactivateButton1();
activateButton2();
}
function button2() {
deactivateButton2();
activateButton1();
}
答
合并onclicks下面用正确的起始值按钮的disabled属性
$('#button1').click(function(){
$('#button2').attr('disabled','');
$('#button1').attr('disabled','disabled');
}
$('#button2').click(function(){
$('#button1').attr('disabled','');
$('#button2').attr('disabled','disabled');
}
答
如果你永远只能将有两个按钮,你真的可以只是这样做:
$('button').click(function(){
$(this).attr('disabled', true).siblings('button').attr('disabled', false);
return false;
});
答
对于不适用略去状态尽量使用属性禁用如下:
$('id_button_2').disabled=false;
然后,当你想激活其他按钮使用:
$('id_button_1').disabled=true;
请记住,你必须管理的事件功能(例如这种状态。 onclick)取决于你想要的东西
以前从未想过这个解决方案! :D很好 – Jordy 2012-05-31 08:49:57