如果确认对话框为“取消",则中止Ajax请求. [英] Aborting an Ajax request if confirmation dialog is "Cancel"

查看:92
本文介绍了如果确认对话框为“取消",则中止Ajax请求.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个<f:ajax>组件,该组件将显示一个确认对话框,然后如果选择了OK则显示action标记,或者如果选择了Cancel则中止. 我以为这可以用,但是不行(无论我在确认中选择什么,ajax部分都可以运行):

I'd like to have a <f:ajax> component that would display a confirmation dialog and then do the action tag if OK was selected or abort if Cancel was selected. I thought this would work, but it doesn't (The ajax part runs no matter what I select in the confirmation):

<f:ajax render="some_div">
<h:commandButton value="A Hazardous Button!"
  onclick="show_confirm()" action="#{bean.doSomething}" />
</f:ajax>

-

function show_confirm()
{
    var r=confirm("Are you sure you want to do this? This is totally hazardous!");
    if (r==true)
    {
      return true;
    }
    else
    {
      return false;
    }
}

知道为什么吗?

推荐答案

首先,您需要将return关键字添加到onclick处理程序中...尝试将onclick="show_confirm()"更改为onclick="return show_confirm()"

Firstly you need to add the return keyword to your onclick handler... try changing onclick="show_confirm()" to onclick="return show_confirm()"

第二,您可以将功能简化为:

secondly, you can simplify your function to:

function show_confirm()
{
    return confirm("Are you sure you want to do this? This is totally hazardous!");
}

这篇关于如果确认对话框为“取消",则中止Ajax请求.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆