CakePHP Javascript确认对话框表单提交取消不工作 [英] CakePHP Javascript Confirm dialog Form Submission cancel not working

查看:291
本文介绍了CakePHP Javascript确认对话框表单提交取消不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我的CakePHP的注册表单,一旦点击提交按钮,我只想显示Javascript确认对话框,它应该像:

With my CakePHP's registration form, once clicking Submit button, I simply want to display Javascript Confirm dialog box, which should work like:


  • 如果按下确定,请提交表单

  • 如果按取消,不应去提交操作

但在这里,当我按取消,虽然它被提交。不知道为什么?

But here, when i press Cancel, though it gets submitted. Don't know why?

CakePHP表单代码:

CakePHP Form Code:

&ph? echo $ form-> create('Noncompetitor',array('type'=>'file','url'=>'/ register','onSubmit'=>'confirmfrmSubmit();')); ?>

我的JS代码:

`function confirmfrmSubmit {

`function confirmfrmSubmit(){

var agree=confirm("Are you sure you wish to continue?");

if (agree)
    return true ;
else
    return false ;

}
`

谢谢!

推荐答案

更新:归功于 自行车 修复我破碎的回调。有关详细信息,请参见下文。 (和一个大树莓给StackO,不允许答案作者对接受编辑的最后说法。)

Update: Credit to bicycle for fixing my broken callback. See below for details. (And a big raspberry to StackO for not allowing the answer author to have the final say on accepting an edit.)

这里是使用jQuery的解决方案:

Here's a solution using jQuery:

<?php
    $this->Html->scriptBlock("
        jQuery(function($){
            $('form.noncompetitor').submit(function(event){
                if (!confirm('Are you sure?')) { return false; }
            });
        });
    ",array('inline'=>false)); // inline => false will put this snippet in the DOM's head.

    echo $this->Form->create('Noncompetitor',array(
        'type'=>'file','url'=>'/register',
        'default'=>false,'class'=>'noncompetitor'
    ));

    /* The rest of your form */

这篇关于CakePHP Javascript确认对话框表单提交取消不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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