简体   繁体   English

帮助javascript确认对话框

[英]help with javascript confirmation dialog

So here's my situation: I have a form that validates with PHP. 所以这是我的情况:我有一个使用PHP验证的表单。 I want to make it so that if the form fails validation, the user is forced to click through a confirmation dialog before they navigate to another page (the form is fairly large and they don't want to accidentally leave it before it's saved). 我要这样做,以便如果表单无法通过验证,则用户将被迫单击确认对话框,然后再导航至另一页面(表单相当大,他们不希望在保存表单之前无意离开它)。 I'm going about this like so: 我正在这样的事情:

see updated function below , 请参阅下面的更新功能

Basically use php within the function to either set the body to present the confirmation or do nothing depending on the error status of my form. 基本上在函数中使用php来设置正文以显示确认,或者根据我的表单的错误状态不执行任何操作。 Nothing happens when the form isn't submitted and I click a link, good. 没有提交表单时什么也没有发生,我单击一个链接,很好。 When the form is displaying errors and I click a link the confirmation dialog will appear but canceling it causes it to reappear. 当表单显示错误并单击链接时,将显示确认对话框,但取消对话框将使其重新出现。 If I cancel it a second time the page request will go through even though it's not supposed to. 如果我第二次取消它,即使不应该执行该页面请求。 I'm not that familiar with javascript so I'm not sure what's going on. 我对javascript不太熟悉,所以我不确定发生了什么。 Is there a better way I should be going about this? 我应该有更好的方法吗?

Edit: I figured it out , it was a combination of things. 编辑: 我想通了 ,这是事情的结合。 The first was a really dumb mistake on my part: I was calling the onlick on both tags AND the tags for each link in my list, hence why the box popped up twice. 首先,我确实犯了一个愚蠢的错误:我在标签和列表中每个链接的标签上都调用了onlick,因此为什么该框会弹出两次。

the second piece was that even though my function already returns bool, the onclick requires an explicit return declaration. 第二部分是,即使我的函数已经返回布尔值,onclick仍需要显式的返回声明。 I was doing: 我在做:

<a onclick="forceConfirm();" href="somepage.html">Blah</a>

When it should have been: 应该是什么时候:

<a onclick="return forceConfirm();" href="somepage.html">Blah</a>

Then just edit the PHP so that forceConfirm always returns true when the form hasn't been submitted, bypassing the confirmation: 然后只需编辑PHP,以使forceConfirm始终在未提交表单时返回true,从而绕过确认:

            function forceConfirm(){
            <?php
                if($form->errorStatus){
                    echo 'if(confirm("Are you sure you want to navigate away from this page? All unsaved changes will be lost.")){'."\n".
                        'return true;'."\n".'}'."\n".
                        'else{ return false;}';
                }
                else{ echo 'return true;';}
            ?>
        }

Now I just need to figure out how to use jQuery to apply this to all links without having to put onclick events all over the place.... 现在,我只需要弄清楚如何使用jQuery将其应用于所有链接,而不必到处放置onclick事件。

You can use confirm() like this: 您可以像这样使用confirm()

if(confirm("Are you sure you want to proceed?")){
    document.location = 'confirmed redirect url...';
}
else{
    document.location = 'cancel redirect url...';
}

Then you'd wrap that in the same PHP block as in your example, displaying it if necessary and hiding it if not. 然后,将其包装在与示例相同的PHP块中,如果需要,将其显示,如果没有,则将其隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM