javascript 禁用 input type="image" 作为按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17933295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:09:58  来源:igfitidea点击:

Disabling input type="image" as button

javascriptjqueryhtmlcss

提问by user2605321

I have an image as a button..I want to disable the image onclick...How can I do that?

我有一个图像作为按钮..我想在点击时禁用图像...我该怎么做?

<input type="image" src="someimage.png" height="20px" width="20px"  name="button" />

回答by Mihail

html(add id):

html(添加ID):

<input id="btn" type="image" src="someimage.png" height="20px" width="20px"  name="button" />

js:

js:

document.getElementById("btn").onclick = function(){
  this.disabled = true
}

http://jsfiddle.net/Dtgb4/

http://jsfiddle.net/Dtgb4/

Or you can just add attribute onclick="this.disabled = true"

或者你可以添加属性onclick="this.disabled = true"

update for comment:

更新评论:

You can use CSS rule :disabledfor styling disabled buttons

您可以使用 CSS 规则:disabled来设置禁用按钮的样式

http://jsfiddle.net/Dtgb4/1/

http://jsfiddle.net/Dtgb4/1/

P.S. pseudo-class :disabled only for modern browsers(not for IE8-).

PS 伪类:仅对现代浏览器禁用(不适用于 IE8-)。

回答by v2b

Unless you have a specific reason to use the input type, you shouldn't be using it for image.

除非您有使用输入类型的特定原因,否则不应将其用于图像。

<img src="smiley.gif" alt="Smiley face" height="42" width="42">

use the image type, and no onclick handler.

使用图像类型,并且没有 onclick 处理程序。

回答by rodolfoprado

You can use

您可以使用

<button height="20px" width="20px"  name="button"><img src="someimage.png"/></button>

and now you can disable button as normal as everything

现在你可以像平常一样禁用按钮