我有一个 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
监听器没有被调用。
有条件地允许默认事件是我提出的最佳解决方案,但有几个可能的选项:
- 是否有其他方法可以从 Javascript 中触发 Tapestry 4 侦听器?
- 有没有办法识别我的 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:
- Is there some other way to cause a Tapestry 4 listener to be fired from within Javascript?
- 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.
发布评论
评论(3)
不要。
您应该检查点击处理程序中的条件并在那里调用 stopEvent,如下所示:
Don't.
You should rather check the conditions in the click handler and call stopEvent there like so:
Internet Explorer不支持点击。您应该使用 fireEvent 方法,例如,
这应该适用于 IE。对于其他浏览器,我想点击就可以了。无论如何,如果我应该做类似的任务,我会尝试为 Tapestry 编写一个适配器并使用 Tapestry javascript 库。
Internet explorer does not support click. You should use fireEvent method instead e.g.
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.
Form
组件上有一个listener
参数;来自 Tapestry 4 文档:将此参数设置为我的侦听器方法,如下所示:
导致 Tapestry 表单提交
触发侦听器。
There's a
listener
parameter onForm
components; from the Tapestry 4 doc:Setting this parameter to my listener method like so:
causes a Tapestry form submit
to trigger the listener.