怎么在javascript里让一个按钮失效

JavaScript014

怎么在javascript里让一个按钮失效,第1张

通过disabled属性职位true设置按钮不可用。

1、获取按钮对象

2、设置按钮对象的disabled的属性为true(禁用),false(不禁用)

示例:

btn=document.getElementById('按钮的ID')

btn.disabled=true

第一种方法:直接按钮中加入

当点击提交后,提交按钮变灰色不可用,这样可有效防止重复提交,本代码就是实现这样一个功能。从代码就可以看出,我们只需在提交按钮上加入这一句: onclick="javascript:{this.disabled=truedocument.form1.submit()}",意思是当按钮点击后,将按钮的不可用属性设置为true,这样按钮就变灰了

<html>

<head>

<meta http-equiv="Content-Type" content="text/html charset=gb2312">

<title>表单提交后按钮变成灰色</title>

</head>

<body>

<form name=form1 method="POST" action="/" target=_blank>

<p><input type="text" name="T1" size="20"><input type="button" value="提交" onclick="javascript:{this.disabled=truedocument.form1.submit()}">

<input type="reset" value="重置" name="B2"></p>

</form>

</body>

</html>

第二种方法:通过onSubmit事件实现,并且可以将变灰按钮变为可用

在form里面添加 onSubmit事件,如果表单加入了判断,那么这个方法直接就可以用了,记住就放到最后,否则一开始就为灰了,但我们加上了一个使提交按钮变为可用的代码,

即可防止重复提交信息,也可以防止代码问题导致不可提交的情况

<form name=form1 action="" onSubmit=" return closebut()" >

<input name="imageField" type="submit" class="inputbut" value="确定" /><br>

<input type="button" name="hui" id="hui" value="让提交按钮可用" onclick="document.form1.imageField.disabled=false" />

</form>

<script>

function closebut(){

document.form1.imageField.disabled=true

}

</script>

第三种,跟上面的类似

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="content-type" content="text/htmlcharset=gb2312">

<meta name="keywords" content="站长,网页特效,js特效,js脚本,脚本,广告代码" />

<meta name="description" content="www.jb51.net,站长特效网,站长必备js特效及广告代码。大量高质量js特效,提供高质量广告代码下载,尽在站长特效网" />

<title>网页特效 表单提交后按钮变灰效果三</title>

</head>

<body>

<a href="http://www.jb51.net/">脚本之家</a>,站长必备的高质量网页特效和广告代码。<hr>

<!--欢迎来到脚本之家,我们网站收集大量高质量js特效,提供许多广告代码下载,网址:<A href="http://www.jb51.net">www.jb51.net</A>,用.net打造靓站-->

<script language="javascript">

function submitonce(jb51_net){

 if(document.all||document.getElementById){

  for(i=0i<jb51_net.lengthi++){

   var tempobj=jb51_net.elements[i]

   if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")

   tempobj.disabled=true

  }

 }

}//欢迎来到站长特效网,我们的网址是www.jb51.net,很好记,zz站长,js就是js特效,本站收集大量高质量js代码,还有许多广告代码下载。

</script>

<form action="http://www.jb51.net" method="post" name="jb51_net" onSubmit="submitonce(this)">

<input type="text" name="name">

<input type="submit" name="submit1" value="提交">

</form>

</body>

</html>

希望按钮不可以用,可以直接修改这个按钮的disabled属性就可以了。

下面是简单的代码实现,仅供参考:

<body>

    <input type="button" id="btn" value="click" />

</body>

<script>

    var oBtn = document.getElementById('btn')

    oBtn.onclick = function(){

        oBtn.disabled = 'disabled'

    }

</script>