简体   繁体   English

禁用输入字段上的jQuery单击事件

[英]jQuery click event on disabled input field

I want to trigger an event on a disabled input .我想在禁用的input上触发事件。 I have tried the following code, nothing happened - there was no error, the even just didn't fire.我尝试了以下代码,没有任何反应 - 没有错误,甚至没有触发。

$(document).on('click', '.select-row', function(e){
    ...
});

How do I fix this?我该如何解决?

You can't fire any mouse event like click on a disabled HTML element. 您不能触发任何鼠标事件,例如单击禁用的HTML元素。 Alternative of this, is to wrap up the element in a span or div and perform the click event on those. 替代方法是将元素包装在span或div中,然后对它们执行click事件。

$("div").click(function (evt) {
  // do something
});​

One more alternate is to make the element readonly instead of disabled , but bear in mind that jQuery does not have a default :readonly selector. 另一种替代方法是使元素变为readonly而不是disabled ,但是请记住,jQuery没有默认的:readonly选择器。

If it works for you, you may use readonly instead of disabled . 如果适合您,则可以使用readonly而不是disabled Then you can use the click event with ease. 然后,您可以轻松使用click事件。

Demo 演示

We had today a problem like this, but we didn't wanted to change the HTML. 今天,我们遇到了这样的问题,但是我们不想更改HTML。 So we used mouseenter event to achieve that 所以我们使用mouseenter事件来实现

var doThingsOnClick = function() {
    // your click function here
};

$(document).on({
    'mouseenter': function () {
        $(this).removeAttr('disabled').bind('click', doThingsOnClick);
    },
    'mouseleave': function () {
        $(this).unbind('click', doThingsOnClick).attr('disabled', 'disabled');
    },
}, '.select-row');

This my solution这是我的解决方案

<div class="myInput"><input type="text" value="" class="class_input" disabled=""></div>

Jquery:查询:

$("body").on("click", ".myInput", function(e) {
  if($(e.target).closest('.class_input').length > 0){
     console.log('Input clicked!')
  }
});

如果需要触发,请使用trigger()函数。

$('.select-row').trigger('click');

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

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