检查按钮是否被禁用,然后警告 HTML Onclick
现在我有一个复选框可以启用/禁用我的表单的提交按钮。
我只是有这个用于启用/禁用它的代码,
onclick="if(this.checked){this.form.submit_btn.disabled=false}else{this.form.submit_btn.disabled=true};this.blur();"
现在我只是想知道如何制作它,以便如果他们在禁用按钮时单击按钮,它会用特定消息提醒他们。
那么向按钮添加一个 onclick 事件并让它像
if(this.disabled=true){alert('Button disabled!');}
或类似的东西吗?我对所有 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
if(this.disabled=true){alert('Button disabled!');}
or something similar? I'm pretty new to all the onclick stuff.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不知道它的价值是什么,但在 jQuery 中它会是这样的:
不过看不出有什么意义。当按钮已经被禁用时(浏览器也显示如此),为什么还要用消息来打扰别人呢?
Don't know what it's worth but in jQuery it would go like:
Don't see the point though. Why bother someone with a message while the button is already disabled (and the browser is showing so).
无论如何你不能,至少不能直接。当按钮被禁用时,它就被禁用,这意味着它也不会触发任何事件。
现在..也就是说,如果你绝对有必要的话,有一种方法可以绕过它。
本质上,您可以通过使用 2 个 div 和一个按钮来使其工作,如下所示:
将覆盖 div 的背景颜色设置为透明,当您禁用该按钮时,您将取消隐藏覆盖 div。
因为 div 是透明的,所以您应该看到按钮中没有任何变化,但单击它会导致透明 div 首先拾取点击次数。
然后,您可以将单击处理程序附加到覆盖 div,结果是您可以通过单击禁用按钮触发一条显示(但实际上不是)弹出的消息。
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:
You set the overlay div's background colour to transparent, and when you disable the button, you unhide the overlay 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.
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.
相反,您可以使用灰度方法更改图像样式。
Instead you may change the image style with gray scale approach.