JSF:使用的onclick +的ActionListener在一起JSF、onclick、ActionListener

2023-09-10 16:33:13 作者:灑脱又极端

我想按钮点击执行JavaScript,并在同一时间在底层设置属性。执行JS code工作,但属性的动作监听不会被调用。当我删除了onclick属性,但属性是在底层单击命令按钮时设置。我也试着使用一个ActionListener。但同样的问题。这是我的JSF code:

I'm trying to execute JavaScript on button click and setting properties in the backing at the same time. Executing JS code works, but the property action listener does not get called. When I remove the onclick attribute, however, the properties are set in the backing when clicking the command button. I've also tried using an actionListener. But same issue. Here's my JSF code:

<rich:dataTable value="#{bean.items}" var="item">
  <rich:column>
    <a4j:commandButton value="Upload" onclick="Richfaces.showModalPanel('p-id');return false;">
       <f:setPropertyActionListener target="#{bean.targetId}" value="#{item.id}" />   
   </a4j:commandButton>
 </rich:column>
 // more columns
</rich:dataTable>

我缺少的东西here`?

Am I missing something here`?

推荐答案

&LT; A4J:的commandButton&GT; 组件生成一个HTML &LT;输入TYPE =提交&GT; 连同一堆JS code的AJAX魔术是不相关的当前问题的元素

The <a4j:commandButton> component generates a HTML <input type="submit"> element along with a bunch of JS code for the ajax magic which is not relevant to the current question.

组件的的onclick 属性获取生成的非常相同的HTML元素的的onclick 属性。该的onclick 属性通常是指一些JavaScript code这得到执行时,HTML DOM 点击事件被激发的特别是HTML元素。该事件被触发时,立即终端用户点击HTML元素,这发生的 的HTML元素的默认操作(提交父窗体)被调用之前。更有甚者,默认操作可以通过JavaScript的code布尔结果进行控制。如果返回,则默认操作将不被执行。这就是发生在你的情况。

The component's onclick attribute get generated as the onclick attribute of the very same HTML element. The onclick attribute usually refers some JavaScript code which get executed when the HTML DOM click event is fired on the particular HTML element. This event is fired immediately when the enduser clicks the HTML element and this takes place before the HTML element's default action (submitting the parent form) is invoked. Even more, the default action can be controlled by the boolean outcome of the JavaScript code. If it returns false, then the default action will not be performed. This is what is happening in your case.

如果你想同时调用JavaScript的code。在的onclick 属性的和的的HTML的默认操作&LT;输入类型=提交&GT; 元素,那么你必须返回,或者只是删除返回false; 片完全

If you want to invoke both the JavaScript code in the onclick attribute and the default action of the HTML <input type="submit"> element, then you have to return true, or just to remove the return false; piece altogether.