jQuery检测点击禁用提交按钮 [英] jQuery detect click on disabled submit button

查看:103
本文介绍了jQuery检测点击禁用提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小提琴: http://jsfiddle.net/ugzux/



正如您所看到的,我有一个禁用(通过JavaScript)提交按钮的表单。



我希望能够将点击事件绑定到它,所以我可以做一些爵士乐的指示,说明需要在输入上修正什么,然后才允许然后,禁用提交按钮也显然禁用绑定到该按钮的任何点击事件,即使它们绑定了也是如此(即使绑定了该按钮)。

禁用之后 - 任何想法如何解决这个问题?



实际上,一种解决方案是停止禁用按钮,而不是有一个事件

  $('form')。submit(function(event){
event.preventDefault();
});

然而,我想知道禁用的输入和JavaScript事件的来龙去脉,如果有变通方法因为我以前从未遇到过这种情况。

解决方案




Firefox和其他浏览器可能会禁用已禁用的表单域
上的DOM事件。任何以禁用表单字段开头的事件都是完全取消的
,并且不会向上传播DOM树。如果我错了,纠正我
,但如果你点击禁用按钮,
的来源是禁用按钮,点击事件完全是
。浏览器从字面上不知道被点击的按钮,
也没有通过点击事件。就好像你点击了网页上的
黑洞。


我以为你可能会通过在div中包装按钮并触发div的click事件的逻辑来伪造点击。但是,如上所述,禁用元素上的事件似乎没有冒泡到DOM树上。

Fiddle: http://jsfiddle.net/ugzux/

As you can see, I have a form with a disabled (via javascript) submit button.

I want to be able to bind a click event to it anyway, so I can do some jazzy indication of what needs to be fixed on the input before I'll allow the form to be submitted (i.e enable the button again).

However, disabling the submit button also apparently disables any click events bound to the button, even if they are bound after the disable - any idea how to get around this?

Practically, one solution is to stop disabling the button and instead have an event that does

$('form').submit(function(event){
    event.preventDefault(); 
});

However I want to know the ins and outs of disabled inputs and javascript events, and if there are workarounds as I've never encountered this behaviour before.

解决方案

Found this in this question -

Firefox, and perhaps other browsers, disable DOM events on form fields that are disabled. Any event that starts at the disabled form field is completely canceled and does not propagate up the DOM tree. Correct me if I'm wrong, but if you click on the disabled button, the source of the event is the disabled button and the click event is completely wiped out. The browser literally doesn't know the button got clicked, nor does it pass the click event on. It's as if you are clicking on a black hole on the web page.

I'd thought you might be able to 'fake' a click by wrapping the button in a div and firing the logic on the div's click event. But, as indicated above, the events on disabled elements do not seem to be bubbled up the DOM tree.

这篇关于jQuery检测点击禁用提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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