简体   繁体   English

禁用输入上的OnClick事件

[英]OnClick Event on a disabled input

  • I need to have an onclick event on an <input> tag which is disabled. 我需要在<input>标签上有一个onclick事件,该事件被禁用。
  • Here onclick event doesn't work. 这里onclick事件不起作用。
  • Is there any other way to work with onclick event for disabled input based on id? 有没有其他方法可以使用基于id的禁用输入的onclick事件?
  • I tried the code below. 我试过下面的代码。
  • First input worked but I need worked same as second one also same as of first one. 第一次input工作但我需要与第二次input相同,也与第一次input相同。 (I need to call function Clicked on input only). (我需要调用函数Clicked on input only)。
  • thanks in advance.. 提前致谢..

My code: 我的代码:

 function Clicked(event) { alert(event.id) } function ClickedDisabled(event) { alert(event.ids) } 
 <input type="text" id="ID" onclick="Clicked(this)" /> <input type="text" id="IDs" onclick="ClickedDisabled(this)" disabled /> 

 function Clicked(event) { alert(event.id) } function ClickedDisabled(event) { alert(event.id) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <input type="text" id="ID" onclick="Clicked(this)" /> <span style="position:relative;"> <input type="text" id="IDs" disabled /> <div style="position:absolute; left:0; right:0; top:0; bottom:0; cursor: pointer;" id="IDs" onclick="ClickedDisabled(this)"></div> </span> 

Try this it may help you 试试这可能会对你有所帮助

Put

input[disabled] {pointer-events:none}

in your CSS (it prevents some browsers from discarding clicks on disabled inputs altogether), and capture the click on a parent element. 在您的CSS中(它可以防止某些浏览器完全放弃对已禁用输入的点击),并捕获父元素上的点击。 It's a cleaner solution, IMHO, than putting a transparent overlay over the element to capture the click, and depending on circumstances may also be much easier than simply "simulating" the disabled state using CSS (since that won't prevent the input from being submitted, and also requires overriding the default browser 'disabled' style). 这是一个更清洁的解决方案,恕我直言,而不是在元素上放置透明覆盖来捕获点击,并且根据情况也可能比使用CSS简单地“模拟”禁用状态容易得多(因为这不会阻止输入提交,还需要覆盖默认浏览器“禁用”样式。

If you have multiple such buttons, you'll need a unique parent for each, in order to be able to distinguish which button was clicked, because with pointer-events:none , the click target is the button's parent rather than the button itself. 如果您有多个这样的按钮,则每个按钮都需要一个唯一的父按钮,以便能够区分单击了哪个按钮,因为使用pointer-events:none ,单击目标是按钮的父按钮而不是按钮本身。 (Or you could test the click coordinates, I suppose...). (或者你可以测试点击坐标,我想......)。

If you need to support older browsers, though, do check which ones support pointer-events : http://caniuse.com/#search=pointer-events 但是,如果您需要支持旧版浏览器,请检查哪些支持pointer-eventshttp//caniuse.com/#search=pointer-events

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

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