启用或禁用javascript中的按钮

时间:2016-02-22 13:50:45

标签: css3

我试图通过javascript控制输入类型按钮标记的禁用状态。我使用的浏览器是Chrome。我已经做了一个jsfiddle来模拟这个问题,有一个切换按钮,它应该在禁用的true和false状态之间切换,并且它专门针对停止按钮。 这是代码:

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 {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 5px;
	font: inherit;
	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%;	
  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{
	text-transform: uppercase;
	color:grey;
	font-weight:bold;
	font-size: 12px;
	padding:5px;
	text-align: center;
}
.WebRecallForm h2, .NoteForm h2 {
	text-transform: uppercase;
	padding:5px;
	color:#5e9dd1;
	font-weight:bold;
	font-size: 11px;
	margin-bottom: 10px;
	text-align: center;
}
.NoteForm h2 {
	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 { 
	padding: 5px;
	height:28px;
}
.WebRecallForm label {
	float: left;
}
.WebRecallForm li input, number {
	height: 21px;
	float: right;
	margin-right: 10px;
	padding-left: 5px;
	padding-right: 5px;
}
.WebRecallForm li input, number, select {
	height: 23px;
	border-radius: 3px;
	font-family: Tahoma;
    font-size: 11px;
}
.WebRecallForm input[type=number] {
	font-size: 11px;
}
.WebRecallForm input[type=password], input[type=text], input[type=select], input[type=number] {
    padding: 5px;
    width: 250px;
    margin-right: 10px;
	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]  {
	height: 15px;
	width: 15px;
	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;
}
#period, #encoding, #nrchannels, #size, #periodRadius {
	width: 150px;
	float: right;
	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 {
	color: red;
	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*/
	border-radius: 3px;
	color:white;
	border:1px solid white;
	text-transform: uppercase;
	margin-left: 15px;
}
.WebRecallForm input[type=submit]:hover, input[type=button]:hover, .NoteForm input[type=submit]:hover {
	border: 1px solid #639FD2;
	/*reverse the gradient colours*/
	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, #5e9dd1, #a5c4db);
	background-image: -webkit-gradient(linear, center top, center bottom, from(#5e9dd1), to(#a5c4db));
	background-image: -webkit-linear-gradient(top, #5e9dd1, #a5c4db);
	background-image: linear-gradient(top, #5e9dd1, #a5c4db);
}
.WebRecallTable {
	border-radius: 5px;
	border-spacing: 10px;
	background:#ECEEE9;			  
	font-family: Tahoma;
	font-size: 12px;
	font-color: grey;
	padding: 10px;
}
.WebRecallTable td {
	padding-left: 10px;
	padding-bottom: 20px;
}
.WebRecallTable th{
	font-weight: bold;
	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 {
	position: fixed;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background: rgba(0,0,0,0.2);
	z-index: 99999;
	opacity:0;
	-webkit-transition: opacity 400ms ease-in;
	-moz-transition: opacity 400ms ease-in;
	transition: opacity 400ms ease-in;
	pointer-events: none;
}
.ModalWindow:target {
	opacity:1;
	pointer-events: auto;
}
.ModalWindow > div {
	width: 50%;
	position: relative;
	margin: 10% auto;
	-webkit-border-radius: 5px;
	-moz-border-radius: 5px;
	border-radius: 5px;
	background:#ECEEE9;
	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 {
	background: #F1F1F1;
	border-top: 1px solid #999;
	-moz-box-shadow: inset 0px 13px 12px -14px #888;
	-webkit-box-shadow: inset 0px 13px 12px -14px #888;
	box-shadow: inset 0px 13px 12px -14px #888;
    }
.modalFooter p {
	color:#D4482D;
	text-align:right;
	margin:0;
	padding: 5px;
}
.ok, .close, .cancel {
	background: #606061;
	color: #FFFFFF;
	line-height: 25px;
	text-align: center;
	text-decoration: none;
	font-weight: bold;
	-webkit-border-radius: 2px;
	-moz-border-radius: 2px;
	border-radius: 2px;
	-moz-box-shadow: 1px 1px 3px #000;
	-webkit-box-shadow: 1px 1px 3px #000;
	box-shadow: 1px 1px 3px #000;
}
.close {
	position: absolute;
	right: 5px;
	top: 5px;
	width: 22px;
	height: 22px;
	font-size: 10px;
}
.ok, .cancel {
	width:80px;
	float:right;
	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;
	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*/
		border-radius: 3px;
		color:white;
		border:1px solid white;
		text-transform: uppercase;
}

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

2 个答案:

答案 0 :(得分:2)

您将禁用值设置为STRING“true”或“false”。如果你把它改成布尔 - 只有正确或错误 - 应该工作

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

答案 1 :(得分:1)

为了更好地与浏览器兼容,请将值设置为&#34;禁用&#34; e.g。

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