如何在事件处理程序中触发元素的默认事件?

发布于 2024-12-11 18:57:53 字数 1724 浏览 0 评论 0 原文

我有一个 HTML 按钮,需要检查几个条件,如果它们通过,则允许发生默认操作。

以下内容在 Firefox 中有效,但在 IE 中失败。我在按钮上设置了一个单击处理程序:

Ext.get('send').on('click', handleSend, this, {
    preventDefault: true
});

如果不满足其中一个条件,则会弹出几个消息框之一。如果满足所有条件,我从按钮中删除单击侦听器,然后再次单击按钮:

Ext.get('send').un('click', handleSend, this);

Ext.getDom('send').click();

据我所知,它在 IE(可能还有其他浏览器)中失败,因为 click() 不是 DOM 的标准函数元素。

如果默认操作是一个简单的表单提交,我可以在检查通过后执行此操作,但我们使用带有侦听器的 Tapestry 4,它不会在正常的表单提交上执行。

我尝试提交表单

tapestry.form.submit('composeForm', 'doSend');

,但 doSend 监听器没有被调用。

有条件地允许默认事件是我提出的最佳解决方案,但有几个可能的选项:

  1. 是否有其他方法可以从 Javascript 中触发 Tapestry 4 侦听器?
  2. 有没有办法识别我的 Tapestry 页面中提交的普通表单,从而触发监听器?

JSFiddle 添加

在此 JSFiddle 添加net/Gdges/7/" rel="nofollow">jsfiddle,默认操作是提交表单;当取消选中该复选框时,可以防止这种情况发生。选中后,它会删除处理程序,但对 click() 的调用在 IE 中不起作用。

有没有办法在IE中模拟点击?

更新

问题中的另一个障碍是我必须显示“您确定吗”对话框,因此为了给他们时间回答,必须停止该事件。如果他们单击“确定”,则需要执行默认操作。 JSFiddle 似乎没有像 MessageBox 这样的 ExtJS 小部件,所以我不确定如何演示这种行为。

根据 @Ivan 的建议,我尝试了

Ext.getDom('send').fireEvent('onclick');

,但它返回 false,这意味着该活动在某处被取消。然后我尝试了,

var evt = document.createEvent("Event");
evt.initEvent('click', false, false);
var cancelled = Ext.getDom('send').fireEvent('onclick', evt);

但 IE9 说 document.createEvent 不存在,尽管这就是 MSDN 说要这样做。

I have an HTML button that needs to check several conditions, and if they pass allow the default action to occur.

The following works in Firefox, but it fails in IE. I setup a click handler on the button:

Ext.get('send').on('click', handleSend, this, {
    preventDefault: true
});

which pops up one of several message boxes if one of the conditions isn't met. If all conditions are met, I remove the click listener from the button and click the button again:

Ext.get('send').un('click', handleSend, this);

Ext.getDom('send').click();

As far as I can tell, it fails in IE (and possibly other browsers) because click() isn't a standard function for a DOM element.

If the default action were a simple form submit, I could just do that after the checks pass, but we're using Tapestry 4 with a listener, which doesn't get executed on a normal form submit.

I've tried submitting the form with

tapestry.form.submit('composeForm', 'doSend');

but the doSend listener isn't getting called.

Conditionally allowing the default event is the best solution I've come up with, but there are a couple of options that may be possible:

  1. Is there some other way to cause a Tapestry 4 listener to be fired from within Javascript?
  2. Is there any way to recognize the normal form submit in my Tapestry Page and thereby trigger the listener?

JSFiddle added

In this jsfiddle, the default action is to submit the form; this is prevented when the checkbox is unchecked. When checked it removes the handler, but the call to click() doesn't work in IE.

Is there a way to simulate a click in IE?

Update

Another snag in the problem is that I have to display an 'are you sure' dialog, so in order to give them time to answer, the event has to be stopped. If they click OK, the default action needs to occur. JSFiddle doesn't seem to have ExtJS widgets like MessageBox, so I'm not sure how to demo this behavior.

At @Ivan's suggestion I tried

Ext.getDom('send').fireEvent('onclick');

but it returns false, meaning the event is being cancelled somewhere. I then tried

var evt = document.createEvent("Event");
evt.initEvent('click', false, false);
var cancelled = Ext.getDom('send').fireEvent('onclick', evt);

but IE9 says that document.createEvent doesn't exist, even though this is how MSDN says to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑疤怪 2024-12-18 18:57:53

如果满足所有条件,我将从按钮中删除点击侦听器
然后再次单击按钮:

不要。

您应该检查点击处理程序中的条件并在那里调用 stopEvent,如下所示:

Ext.get('send').on('click', handleClick);

function handleClick(e) {
    if (condition) {
        e.stopEvent();
    }
}

If all conditions are met, I remove the click listener from the button
and click the button again:

Don't.

You should rather check the conditions in the click handler and call stopEvent there like so:

Ext.get('send').on('click', handleClick);

function handleClick(e) {
    if (condition) {
        e.stopEvent();
    }
}
把人绕傻吧 2024-12-18 18:57:53

Internet Explorer不支持点击。您应该使用 fireEvent 方法,例如,

Ext.getDom('send').fireEvent('onclick');

这应该适用于 IE。对于其他浏览器,我想点击就可以了。无论如何,如果我应该做类似的任务,我会尝试为 Tapestry 编写一个适配器并使用 Tapestry javascript 库。

Internet explorer does not support click. You should use fireEvent method instead e.g.

Ext.getDom('send').fireEvent('onclick');

That should work for IE. For other browsers I guess click is ok. Anyway If I should do similar task I'll try to write an adapter for tapestry and use tapestry javascript library.

执着的年纪 2024-12-18 18:57:53

Form 组件上有一个 listener 参数;来自 Tapestry 4 文档

提交表单时调用的默认侦听器。调用
仅当未调用另一个侦听器(成功、取消或刷新)时。

将此参数设置为我的侦听器方法,如下所示:

<binding name="listener" value="listener:doSend" />

导致 Tapestry 表单提交

tapestry.form.submit('myFormId');

触发侦听器。

There's a listener parameter on Form components; from the Tapestry 4 doc:

Default listener to be invoked when the form is submitted. Invoked
only if another listener (success, cancel or refresh) is not invoked.

Setting this parameter to my listener method like so:

<binding name="listener" value="listener:doSend" />

causes a Tapestry form submit

tapestry.form.submit('myFormId');

to trigger the listener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文