禁用文本字段不会触发Click事件? [英] Click event doesn't fire for disabled text field?

查看:71
本文介绍了禁用文本字段不会触发Click事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个禁用的文本字段,我想在单击它时启用它。不幸的是,似乎文本字段的 click()事件在禁用时不会触发。

I have a disabled text field that I want to become enabled when I click it. Unfortunately, it seems that the text field's click() event doesn't fire while it's disabled.

是否存在解决这个问题?

Is there a way around this?

推荐答案

不幸的是,禁用控件也会禁用其事件(按设计)。

Unfortunately, disabling a control also disables its events (it's by design).

要解决此限制,您可以添加< div> 绝对位于< input> ; 字段(更高 z-index )并将 click()事件附加到该字段< DIV> 。点击后,处理< div> 并启用< input>

To work around that limitation, you could add a <div> absolutely positioned above the <input> field (higher z-index) and attach the click() event to that <div>. Once clicked, dispose of the <div> and enable your <input>.

如果您只是想阻止用户更改< input> 的值,请使用 readonly 而不是已禁用。这不会禁用事件处理程序。

If you just want to prevent the user from changing the value of the <input>, use readonly instead of disabled. This will not disable the event handlers.

$('#myInput').attr('readonly', true);

这篇关于禁用文本字段不会触发Click事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆