最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - If a disabled input button is clicked, display message - Stack Overflow

programmeradmin1浏览0评论

I have an input button, which when it is disabled, and someone tries to click it, needs to display an alert.

How is it possible to display a javascript message despite being disabled?

Tried this with no luck:

<input type="submit" onclick="alert('click')" value="Disabled Input Button" disabled/>

I have an input button, which when it is disabled, and someone tries to click it, needs to display an alert.

How is it possible to display a javascript message despite being disabled?

Tried this with no luck:

<input type="submit" onclick="alert('click')" value="Disabled Input Button" disabled/>

Share Improve this question asked Jul 15, 2014 at 14:15 MRCMRC 5533 gold badges10 silver badges32 bronze badges 2
  • See this: stackoverflow./a/7834293/3751213, however you may use mousedown event... – j809 Commented Jul 15, 2014 at 14:18
  • Possible duplicate of stackoverflow./questions/7833854/… – Chris Lam Commented Jul 15, 2014 at 14:24
Add a ment  | 

2 Answers 2

Reset to default 4

Use onmousedown instead of onclick, which is only fired when it is 'allowed' to. Some browsers, particularly Chrome, appear to disable all DOM events when a form element is disabled. While I think this is out of spec, you can use the following workaround:

Instead of using the disabled attribute, use CSS pointer-events to achieve a similar effect, illustrated here:

button.disabled {
    pointer-events:none;    
}

And then just use <button class="disabled"> instead of <button disabled>.

<span  onclick="alert('This input is disabled')">
    <input type="submit" value="Disabled Input Button" disabled/>
</span>

Wrapping it with another tag that has the on click function works.

发布评论

评论列表(0)

  1. 暂无评论