【问题标题】:unexpected whole page refresh using p:commandButton使用 p:commandButton 意外刷新整个页面
【发布时间】:2016-03-25 11:25:29
【问题描述】:

我正在使用下面的代码删除p:datatable 中的行。如果在p:commandbutton 标签内使用 onclick 方法,它会刷新整个页面,否则它工作正常。请给我一些解决方案。

<p:commandButton title="Delete Affiliation" immediate="true" process="@this"
    update="affiliationList" alt="Delete Affiliation" icon="ui-icon-trash"
    action="#{readAuthorDetailsbean.deleteSelectedAffiliation}" style="width:30px"
    onclick="return confirm('Are you sure, Do you want to delete this record?');">
    <f:setPropertyActionListener value="#{affiliation}" 
        target="#{readAuthorDetailsbean.selectedAffilliationListAL}" />
</p:commandButton>

【问题讨论】:

  • 这个 javascript 或 jquery 有什么关系?您希望我们如何尝试重现?请显示minimal reproducible example。您很可能会从操作中返回一些东西
  • 查看完整代码。我添加了 onclick(Java 脚本函数)。当我在删除确认中单击是时,它返回 true。它刷新整个页面。
  • 默认情况下,PrimeFaces 将return false;(由于 ajax)添加到从p:commandButton 组件生成的 html 代码中,因此它不会触发表单提交。如果您将confirm() 添加到onclick,则在用户单击“确定”后,返回值为true 并进行表单提交(正常提交后,而不是ajax - 因此页面刷新)。你可以试试p:confirmDialog

标签: javascript jsf primefaces


【解决方案1】:

如果您将onclick 属性改写为
if (!confirm('Are you sure, Do you want to delete this record?')) return false;
那么它应该按预期工作。它的作用是:

  • 如果用户确认它没有返回值并且 PrimeFaces 可以进一步处理 AJAX 请求
  • 如果用户取消确认,则返回值为false 并中止进一步处理。

如果您只是执行return confirm('Are you sure?') 并且用户确定,则返回的true 值会取消在onclick 属性末尾添加return false 的PrimeFaces(它为ajax 执行此操作),而不是您看到的ajax 请求正常的帖子提交(页面重新加载)。
return false cancels default behavior of html elements.

【讨论】:

猜你喜欢
  • 2016-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多