2016-02-22 129 views
0

我想通過javascript來控制輸入類型按鈕標記的禁用狀態。我使用的瀏覽器是Chrome。我已經做了一個jsfiddle來模擬這個問題,有一個切換按鈕應該在禁用狀態的真實狀態和錯誤狀態之間切換,並且它專門針對停止按鈕。 下面是代碼:啓用或禁用javascript中的按鈕

function toggleState() { 
 
    if(document.getElementById("stopButton").disabled){ 
 
    document.getElementById("stopButton").disabled = "false"; 
 
    } else { 
 
    document.getElementById("stopButton").disabled = "true"; 
 
    } 
 
}
/* code to reset the browser - compliments to meyerweb */ 
 
html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t border: 0; 
 
\t font-size: 5px; 
 
\t font: inherit; 
 
\t vertical-align: baseline; 
 
} 
 
* { 
 
    -webkit-box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    box-sizing: border-box; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.WebRecallForm { 
 
    display: inline-block; 
 
    vertical-align: top; 
 
    border: 1px solid #999; 
 
    border-radius: 3px; 
 
    margin-left: 30%; \t 
 
    margin-top: 10px; 
 
    margin-bottom: 10px; 
 
    font-family: Tahoma; 
 
    color:grey; 
 
    font-size: 12px; 
 
    padding: 10px; 
 
    padding-bottom: 35px; 
 
    background:#ECEEE9; 
 
} 
 
.NoteForm { 
 
    display: inline-block; 
 
    font-family: Tahoma; 
 
    color:grey; 
 
    font-size: 12px; 
 
    width: 100%; 
 
    padding-right: 15px; 
 
    margin-right: 15px; 
 
} 
 
.WebRecallForm h1, .NoteForm h1{ 
 
\t text-transform: uppercase; 
 
\t color:grey; 
 
\t font-weight:bold; 
 
\t font-size: 12px; 
 
\t padding:5px; 
 
\t text-align: center; 
 
} 
 
.WebRecallForm h2, .NoteForm h2 { 
 
\t text-transform: uppercase; 
 
\t padding:5px; 
 
\t color:#5e9dd1; 
 
\t font-weight:bold; 
 
\t font-size: 11px; 
 
\t margin-bottom: 10px; 
 
\t text-align: center; 
 
} 
 
.NoteForm h2 { 
 
\t text-align: left; 
 
} 
 
#horizonal-line 
 
{ 
 
width: 100%; 
 
margin-top: 15px; 
 
border-top: 1px solid grey; 
 
text-align: center; 
 
} 
 
.WebRecallForm ul { 
 
    list-style-type: none; 
 
} 
 
.WebRecallForm li { 
 
\t padding: 5px; 
 
\t height:28px; 
 
} 
 
.WebRecallForm label { 
 
\t float: left; 
 
} 
 
.WebRecallForm li input, number { 
 
\t height: 21px; 
 
\t float: right; 
 
\t margin-right: 10px; 
 
\t padding-left: 5px; 
 
\t padding-right: 5px; 
 
} 
 
.WebRecallForm li input, number, select { 
 
\t height: 23px; 
 
\t border-radius: 3px; 
 
\t font-family: Tahoma; 
 
    font-size: 11px; 
 
} 
 
.WebRecallForm input[type=number] { 
 
\t font-size: 11px; 
 
} 
 
.WebRecallForm input[type=password], input[type=text], input[type=select], input[type=number] { 
 
    padding: 5px; 
 
    width: 250px; 
 
    margin-right: 10px; 
 
\t margin-left: 15px; 
 
    border: 1px solid grey; 
 
    border-radius: 3px; 
 
    box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, 0.1), 
 
       0px 1px 0px 0px rgba(250, 250, 250, 0.5) ; 
 
    font-family: Tahoma; 
 
    font-size: 12px; 
 
} 
 
.WebRecallForm input[type=checkbox], .NoteForm input[type=checkbox], .NoteForm input[type=radio] { 
 
\t height: 15px; 
 
\t width: 15px; 
 
\t border-radius: 3px; 
 
\t box-shadow: inset 0px 1px 3px 0px rgba(0, 0, 0, 0.1), 
 
       0px 1px 0px 0px rgba(250, 250, 250, 0.5) ; 
 
    font-family: Tahoma; 
 
    font-size: 12px; 
 
} 
 
#period, #encoding, #nrchannels, #size, #periodRadius { 
 
\t width: 150px; 
 
\t float: right; 
 
\t margin-right: 10px; 
 
} 
 
.WebRecallForm input[type=password]:hover, input[type=number]:hover, input[type=text]:hover, select:hover, .NoteTextArea textarea:hover { 
 
    border: 1px solid #639FD2; 
 
} 
 
.errormsg { 
 
\t color: red; 
 
\t text-align: center; 
 
} 
 

 
.wr_button { 
 
text-align: center; 
 
padding: 2px; 
 
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#a5c4db', endColorstr = '#5e9dd1'); 
 
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr = '#a5c4db', endColorstr = '#5e9dd1')"; 
 
background-image: -o-linear-gradient(top, #a5c4db, #5e9dd1); 
 
background-image: -webkit-gradient(linear, center top, center bottom, from(#a5c4db), to(#5e9dd1)); 
 
background-image: -webkit-linear-gradient(top, #a5c4db, #5e9dd1); 
 
background-image: linear-gradient(top, #a5c4db, #5e9dd1); 
 
background-clip: padding-box; 
 
/*Use "background-clip: padding-box" when using rounded corners to avoid the gradient bleeding through the corners*/ 
 
/*--IE9 WILL PLACE THE FILTER ON TOP OF THE ROUNDED CORNERS--*/ 
 
opacity: 0.86; 
 
-ms-filter: progid:DXImageTransform.Microsoft.Alpha(Opacity = 86); 
 
/*-ms-filter must come before filter*/ 
 
\t border-radius: 3px; 
 
\t color:white; 
 
\t border:1px solid white; 
 
\t text-transform: uppercase; 
 
\t margin-left: 15px; 
 
} 
 
.WebRecallForm input[type=submit]:hover, input[type=button]:hover, .NoteForm input[type=submit]:hover { 
 
\t border: 1px solid #639FD2; 
 
\t /*reverse the gradient colours*/ 
 
\t filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#a5c4db', endColorstr = '#5e9dd1'); 
 
\t -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr = '#a5c4db', endColorstr = '#5e9dd1')"; 
 
\t background-image: -o-linear-gradient(top, #5e9dd1, #a5c4db); 
 
\t background-image: -webkit-gradient(linear, center top, center bottom, from(#5e9dd1), to(#a5c4db)); 
 
\t background-image: -webkit-linear-gradient(top, #5e9dd1, #a5c4db); 
 
\t background-image: linear-gradient(top, #5e9dd1, #a5c4db); 
 
} 
 
.WebRecallTable { 
 
\t border-radius: 5px; 
 
\t border-spacing: 10px; 
 
\t background:#ECEEE9; \t \t \t 
 
\t font-family: Tahoma; 
 
\t font-size: 12px; 
 
\t font-color: grey; 
 
\t padding: 10px; 
 
} 
 
.WebRecallTable td { 
 
\t padding-left: 10px; 
 
\t padding-bottom: 20px; 
 
} 
 
.WebRecallTable th{ 
 
\t font-weight: bold; 
 
\t padding: 10px; 
 
} 
 
.WebRecallForm input[type=checkbox], input[type=radio], .NoteForm input[type=checkbox], .NoteForm input[type=radio] { 
 
    vertical-align: middle; 
 
    position: relative; 
 
    bottom: 1px; 
 
} 
 
.WebRecallForm input[type=radio] { 
 
    bottom: 2px; 
 
} 
 
.WebRecallForm input[type=checkbox]:hover , .NoteForm input[type=checkbox]:hover, .NoteForm input[type=radio]:hover{ 
 
    -webkit-box-shadow: inset 0 0 3px 1px #639FD2; 
 
} 
 
.ModalWindow { 
 
\t position: fixed; 
 
\t top: 0; 
 
\t right: 0; 
 
\t bottom: 0; 
 
\t left: 0; 
 
\t background: rgba(0,0,0,0.2); 
 
\t z-index: 99999; 
 
\t opacity:0; 
 
\t -webkit-transition: opacity 400ms ease-in; 
 
\t -moz-transition: opacity 400ms ease-in; 
 
\t transition: opacity 400ms ease-in; 
 
\t pointer-events: none; 
 
} 
 
.ModalWindow:target { 
 
\t opacity:1; 
 
\t pointer-events: auto; 
 
} 
 
.ModalWindow > div { 
 
\t width: 50%; 
 
\t position: relative; 
 
\t margin: 10% auto; 
 
\t -webkit-border-radius: 5px; 
 
\t -moz-border-radius: 5px; 
 
\t border-radius: 5px; 
 
\t background:#ECEEE9; 
 
\t text-align: center; 
 
} 
 
.ModalWindow .modalHeader { padding: 5px 20px 0px 20px; } 
 
.ModalWindow .modalContent { padding: 0px 20px 5px 20px; } 
 
.ModalWindow .modalFooter { padding: 8px 20px 8px 20px; } 
 
.modalFooter { 
 
\t background: #F1F1F1; 
 
\t border-top: 1px solid #999; 
 
\t -moz-box-shadow: inset 0px 13px 12px -14px #888; 
 
\t -webkit-box-shadow: inset 0px 13px 12px -14px #888; 
 
\t box-shadow: inset 0px 13px 12px -14px #888; 
 
    } 
 
.modalFooter p { 
 
\t color:#D4482D; 
 
\t text-align:right; 
 
\t margin:0; 
 
\t padding: 5px; 
 
} 
 
.ok, .close, .cancel { 
 
\t background: #606061; 
 
\t color: #FFFFFF; 
 
\t line-height: 25px; 
 
\t text-align: center; 
 
\t text-decoration: none; 
 
\t font-weight: bold; 
 
\t -webkit-border-radius: 2px; 
 
\t -moz-border-radius: 2px; 
 
\t border-radius: 2px; 
 
\t -moz-box-shadow: 1px 1px 3px #000; 
 
\t -webkit-box-shadow: 1px 1px 3px #000; 
 
\t box-shadow: 1px 1px 3px #000; 
 
} 
 
.close { 
 
\t position: absolute; 
 
\t right: 5px; 
 
\t top: 5px; 
 
\t width: 22px; 
 
\t height: 22px; 
 
\t font-size: 10px; 
 
} 
 
.ok, .cancel { 
 
\t width:80px; 
 
\t float:right; 
 
\t margin-left:20px; 
 
} 
 
.ok:hover { background: #189CDA; } 
 
.close:hover, .cancel:hover { background: #D4482D; } 
 
.clear { float:none; clear: both; } 
 

 
.NoteForm { 
 
    background-color:#ECEEE9; 
 
    font-family: Tahoma; 
 
    color:grey; 
 
    font-size: 12px; 
 
} 
 
.NoteForm ul { 
 
    list-style-type: none; 
 
} 
 
.NoteOptions { 
 
    display: inline; 
 
    width: 10%; 
 
    border: grey; 
 
    float: left; 
 
    padding-left: 15px; 
 
} 
 
.NoteTextArea { 
 
    float: right; 
 
    display: block; 
 
    width: 89%; 
 
} 
 
.NoteTextArea textarea 
 
{ 
 
    height: 120px; 
 
    width: 100%; 
 
} 
 
.NoteButton 
 
{ 
 
    margin-top: 20px; 
 
    margin-bottom: 20px; 
 
    margin-left: 11%; 
 
    float: right; 
 
    text-align: center; 
 
\t padding: 2px; 
 
\t filter: progid:DXImageTransform.Microsoft.gradient(startColorstr = '#a5c4db', endColorstr = '#5e9dd1'); 
 
\t -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr = '#a5c4db', endColorstr = '#5e9dd1')"; 
 
\t background-image: -o-linear-gradient(top, #a5c4db, #5e9dd1); 
 
\t background-image: -webkit-gradient(linear, center top, center bottom, from(#a5c4db), to(#5e9dd1)); 
 
\t background-image: -webkit-linear-gradient(top, #a5c4db, #5e9dd1); 
 
\t background-image: linear-gradient(top, #a5c4db, #5e9dd1); 
 
\t background-clip: padding-box; 
 
\t /*Use "background-clip: padding-box" when using rounded corners to avoid the gradient bleeding through the corners*/ 
 
\t /*--IE9 WILL PLACE THE FILTER ON TOP OF THE ROUNDED CORNERS--*/ 
 
\t opacity: 0.86; 
 
\t -ms-filter: progid:DXImageTransform.Microsoft.Alpha(Opacity = 86); 
 
\t /*-ms-filter must come before filter*/ 
 
\t \t border-radius: 3px; 
 
\t \t color:white; 
 
\t \t border:1px solid white; 
 
\t \t text-transform: uppercase; 
 
} 
 

 
#stopButton:disabled { 
 
\t background-image: -webkit-linear-gradient(top, #AAABAD, #1A2025); 
 
}
<form name="ExportToExcel" id="ExportToExcel" class="WebRecallForm" method="POST"> 
 
\t \t <input type="hidden" name="userGUID" value="${webrecallSession.userGUID}"/> 
 
\t \t <h1>Export to excel</h1> 
 
\t \t <ul> 
 
\t \t \t <li>Rows processed:<input type="text" id="count"/></li> 
 
\t \t \t <li> 
 
\t \t \t \t <input class="wr_button" id="stopButton" name="stopButton" onclick="stopTask()" type="button" value="stop" /> 
 
\t \t \t \t <input class="wr_button" id="downloadButton" name="downloadButton" onclick="showBusy(); document.location='/webrecall/excel?taskID=${taskID}'" type="button" disabled value="Download" /> 
 
\t \t \t \t \t 
 
\t \t \t \t <input class="wr_button" id="backButton" name="backButton" onclick="history.back(-1)" type="button" value="Back" /> 
 
\t \t \t </li> 
 
     <input name="toggleButton" type="button" onclick="toggleState()" value="Click me to toggle"/> 
 
\t \t </ul> 
 
\t </form>

回答

2

你的殘疾的值設置爲字符串「真」或「假」。如果將其更改爲布爾值 - 只是true或false - 應該可以工作

function toggleState() { 
    if(document.getElementById("stopButton").disabled){ 
    document.getElementById("stopButton").disabled = false; 
    } else { 
    document.getElementById("stopButton").disabled = true; 
    } 
} 
1

爲了提高瀏覽器兼容性,請將該值設置爲「disabled」,例如

<input type="button" disabled="disabled" />