JavaScript点击功能不会立即触发IE上的JSF动作方法 [英] JavaScript click function not triggering JSF action method immediately on IE
问题描述
我已经认识到,当通过JavaScript完成点击时,它不会立即在IE上触发动作方法,而是浏览器等待,直到代码完成点击的功能,然后触发动作方法。
I've recognized that when a click is done via JavaScript it won't trigger the action method immediately on IE, instead the browser wait's until the code has gone through the function where click is made and after that triggers the action method.
在Chrome上,点击完成后会立即触发操作方法。
On Chrome the action method is triggered immediately when the click is done.
以下是一个示例xhtml代码:
Here is an example xhtml code:
<h:form>
<ace:pushButton id="button1" action="#{someBean.someFunction1}" label="Button1" />
</h:form>
<h:form>
<ace:pushButton id="button2" action="#{someBean.someFunction2}" label="Button2" />
</h:form>
<h:form>
<ace:pushButton id="button3" action="#{someBean.someFunction3}" label="Button3" />
</h:form>
<h:form>
<ace:pushButton id="button4" action="#{someBean.someFunction4}" label="Button4" />
</h:form>
这是一个示例javaScript代码:
And here is an example javaScript code:
function setTestButtonsOnclickFunctions() {
var button1 = document.querySelector('[id*="button1_button"]');
var button2 = document.querySelector('[id*="button2_button"]');
var button3 = document.querySelector('[id*="button3_button"]');
var button4 = document.querySelector('[id*="button4_button"]');
button1.onclick = function() {
button2.click();
var someRandomVariable = 10;
};
button2.onclick = function() {
var someRandomVariable = 10;
};
button3.onclick = function() {
button4.click();
alert("Button 4 clicked in JavaScript");
var someRandomVariable = 10;
};
button4.onclick = function() {
var someRandomVariable = 10;
};
}
(function($) {
$(document).ready(setTestButtonsOnclickFunctions);
})(jQuery);
注意:
正如你所看到的那样,button3 onclick函数中有一个警告。
因为我认识到当触发警报时触发了动作方法。
但是没有它,在 onclick
函数完成后触发action方法。
As you can see there is an alert in the button3 onclick function.
It is there because I recognized that when the alert is triggered the action method is triggered.
But without it, the action method is triggered after the onclick
function has finished.
还有 someRandomVariable
表示当代码通过 click()
方法时,操作方法不会在IE上触发,而是触发在Chrome上。这使得调试变得容易。
There is also the someRandomVariable
to show that when the code has passed the click()
method the action method isn't triggered on IE, but is triggered on Chrome. This makes it easy to debug.
为什么有两个按钮是因为它是一种简单的方法来测试 click()
方法。
Why there is two buttons is because it is an easy way to test the click()
method.
问题:
- 为什么动作方法不立即在IE上触发?
- 为什么
click()
方法没有触发动作方法在IE上和在Chrome上一样? - 当
click()
方法时,有没有办法在IE上立即触发动作方法在没有使用警报的情况下运行?
- Why doesn't the action method trigger on IE immediately?
- Why doesn't the
click()
method trigger the action method on IE as it does on Chrome? - Is there a way to trigger the action method immediately on IE when the
click()
method is runned, without using an alert?
对于上一个问题,您可能会想到为什么有人甚至想要那个。
For the last question you might wonder why somebody would even want that.
我有一个场景,我希望在窗口关闭时运行一个动作方法。
我使用在JavaScript中使用beforeunload
方法,但由于我上面提到的事情,动作方法永远不会在IE上触发。
Well I have a scenario where I would want to run a action method when the window closes.
I use the beforeunload
method in JavaScript but the action method is never triggered on IE because of the things I mentioned above.
另一个问题:
有没有一种在IE上触发 beforeunload
事件内的动作方法的方法?
Is there a way to trigger an action method inside a beforeunload
event on IE?
推荐答案
//试试这个
(function($) {
setTestButtonsOnclickFunctions()
})(jQuery);
这篇关于JavaScript点击功能不会立即触发IE上的JSF动作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!