如何调用JS函数对JSF按钮和块页面重载/导航点击? [英] How to call JS function on click of JSF button and block page reload/navigation?
问题描述
什么是preferred的方式来创建JSF一个按钮来调用JS功能?
What is the preferred way to create a button with JSF to call JS function ?
其实我是:
<p:button onclick="myJSFunction();" href="#"/>
但我的网址为后缀的锚固符号(#
)。
有另一种推荐的方法来创建按钮,将不会重新加载/导航到URL,但JS调用函数吗?
Is there another recommended way to create button which will not reload/navigate to URL but call JS function please ?
推荐答案
这是因为它的实际导航到#
。您需要执行其默认操作,这是导航到当前视图阻止按钮,或在 HREF指定的网址
或的结果
。您可以通过添加返回false实现这一目标;
到年底的onclick
。
That's because it's actually navigating to #
. You need to block the button from performing its default action, which is navigating to the current view, or to the URL as specified in href
or outcome
. You can achieve this by adding return false;
to the end of onclick
.
<p:button onclick="myJSFunction(); return false;" />
或者,如果 myJSFunction()
实际上返回一个布尔
应该确定按钮应该继续它的默认操作,然后委托给它:
Or, if myJSFunction()
actually returns a boolean
which should determine if the button should continue its default action, then delegate to it:
<p:button onclick="return myJSFunction();" />
的&LT; H:按钮&GT;
以完全相同的方式,它只是在标准look'n'feel
The <h:button>
works exactly the same way, it's only in standard look'n'feel.
另一种方法是使用&LT;电话号码:的commandButton型=按钮&GT;
,产生一个真正的&LT;输入类型=按钮&GT;
无任何导航。这样,您就不需要从返回false 的onclick
。
An alternative is to use <p:commandButton type="button">
, which generates a real <input type="button">
without any navigation. This way you don't need to return false from onclick
.
<p:commandButton type="button" onclick="myJSFunction()" />
然而,这需要一个&LT; H:形式&GT;
(尽管把它放在任何形式之外不会破坏任何功能)。这同样适用于&LT; H:的commandButton&GT;
This however requires a <h:form>
(although placing it outside any form doesn't break any functionality). The same applies to <h:commandButton>
.
这篇关于如何调用JS函数对JSF按钮和块页面重载/导航点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!