button/input/select的disabled屬性
button的disabled屬性
一個被禁用的按鈕:
<button type="button" disabled="disabled">Click Me!</button>
<button type="button" disabled="false">Click Me!</button>
<button type="button" disabled="true">Click Me!</button>
<button type="button" disabled="">Click Me!</button>
注: 無論disabled屬性的值被設置爲什麼值,只要在行內有disabled屬性,該button就不可點擊 。
<body>
<button id="btn1" >點擊</button>
<button id="btn2" disabled="">點擊</button>
</body>
<script>
window.onload=function(){
document.getElementById("btn1").disabled = "";
document.getElementById("btn1").onclick=function(){
alert('按鈕1被點擊了'); //能執行
}
document.getElementById("btn2").onclick=function(){
alert('按鈕2被點擊了'); //不能執行
}
}
</script>
使用javascript禁用按鈕:
document.getElementById("btn1").disabled = true;
document.getElementById("btn1").disabled = 'true'; //只要設置了值,並且值不爲空和false,就會禁用
使用javascript啓用按鈕:
document.getElementById("btn1").disabled = false;
document.getElementById("btn2").disabled = ""; //空字符串,不能有空格
使用jQuery禁用按鈕:
$("#id").attr("disabled","true");
$("#id").attr("disabled",true);
$("#id").attr("disabled","disabled");
$("#id").attr("disabled","");
- 無論disabled的值是什麼,只要設置了disabled的屬性就會被禁用
使用jQuery啓用按鈕:
$("#id").removeAttr("disabled");
$("#id").attr("disabled",false);
- $("#id").attr(“disabled”,“false”);不能啓用按鈕。
- 只有這兩種方式可以重新啓用按鈕。