admin管理员组

文章数量:1296287

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" />

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" />
Share Improve this question asked Jul 29, 2013 at 20:11 user2605321user2605321 932 gold badges5 silver badges11 bronze badges 3
  • 2 disable the image or the input? were do you click? – Sergio Commented Jul 29, 2013 at 20:13
  • The image is acting as a button..So disabling the image would eventually disable the input./button – user2605321 Commented Jul 29, 2013 at 20:13
  • 2 if you're just removing the clickable functionality, why are you using input at all? – user428517 Commented Jul 29, 2013 at 20:16
Add a ment  | 

3 Answers 3

Reset to default 5

html(add id):

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

js:

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

http://jsfiddle/Dtgb4/

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

update for ment:

You can use CSS rule :disabled for styling disabled buttons

http://jsfiddle/Dtgb4/1/

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

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.

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

本文标签: javascriptDisabling input typequotimagequot as buttonStack Overflow