简体   繁体   English

检查按钮是否已禁用,然后提醒HTML Onclick

[英]Check if button disabled then alert HTML Onclick

well right now I have this checkbox that's enabling / disabling a submit button for my form. 现在,我有此复选框,用于启用/禁用表单的提交按钮。

I simply have this code for enabling / disabling it 我只是有这段代码来启用/禁用它

onclick="if(this.checked){this.form.submit_btn.disabled=false}else{this.form.submit_btn.disabled=true};this.blur();"

now I'm just wondering how I can make it so that if they click the button when it's disabled, have it alert them with a certain message. 现在,我只是想知道如何做到这一点,以便如果他们在禁用按钮时单击该按钮,则可以通过一条特定消息提醒他们。

so add an onclick event to the button and have it be like 所以在按钮上添加一个onclick事件,就像

if(this.disabled=true){alert('Button disabled!');}

or something similar? 或类似的东西? I'm pretty new to all the onclick stuff. 我对所有onclick东西都很陌生。

Thanks 谢谢

Don't know what it's worth but in jQuery it would go like: 不知道它的价值,但是在jQuery中它将变成:

// Not tested!    
$(document).ready(function(){
  $('#myButton').click(function(){
     if($(this).is(':disabled')) { 
         alert('No need to click, it\'s disabled.');
     } 
  });
});

Don't see the point though. 虽然看不到重点。 Why bother someone with a message while the button is already disabled (and the browser is showing so). 为什么在按钮已被禁用(浏览器如此显示)的情况下,有人对消息打扰。

You can't well at least not directly anyway. 无论如何,至少不能直接做到这一点。 When a button is disabled, then it's disabled and that means it also fires no events. 当按钮被禁用时,它也被禁用,这意味着它也不会触发任何事件。

Now.. that said, there is a way you could get round it, if you absolutely have to. 那就是说,如果绝对需要,有一种方法可以解决这个问题。

Essentially, you can make it work by using 2 divs and a button like so: 本质上,您可以使用2个div和一个类似的按钮使其工作:

    <div>
      <div id="overlay" style="display:hidden; z-order:99999">&nbsp;</div>
      <button id="mybtn">My Button to click</button>
    </div>

You set the overlay div's background colour to transparent, and when you disable the button, you unhide the overlay div. 将叠加层div的背景色设置为透明,然后在禁用按钮时取消隐藏叠加层div。

Because the div is transparent, you should see NO change in the button, but clicking on it will result in the clicks getting picked up by the transparent div first. 由于div是透明的,因此您应该在按钮上看不到任何变化,但是单击它会导致透明div首先获得点击。

You would then attach your click handler to the overlay div, and the result is that you can trigger a message that appears (but isn't really) to be being popped up by a click on the disabled button. 然后,将点击处理程序附加到叠加层div,结果是您可以触发通过单击禁用按钮弹出(但不是真的)弹出的消息。

Instead you may change the image style with gray scale approach. 相反,您可以使用灰度方法更改图像样式。

img {
  filter: gray; /* IE6-9 */
  filter: grayscale(1); /* Firefox 35+ */
  -webkit-filter: grayscale(1); /* Google Chrome, Safari 6+ & Opera 15+ */
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM