CakePHP Javascript 确认对话框表单提交取消不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5455092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 17:15:23  来源:igfitidea点击:

CakePHP Javascript Confirm dialog Form Submission cancel not working

javascriptcakephpsubmitconfirm

提问by Aditya P Bhatt

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

使用我的 CakePHP 的注册表单,单击“提交”按钮后,我只想显示 Javascript Confirm 对话框,其工作方式如下:

  • If pressed Ok, should submit the form
  • If pressed Cancel, should not go for submit action
  • 如果按确定,则应提交表单
  • 如果按下取消,则不应进行提交操作

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

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

CakePHP Form Code:

CakePHP 表单代码:

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

My JS Code:

我的JS代码:

function confirmfrmSubmit(){

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

    if (agree)
        return true ;
    else
        return false ;
}

Please let me know, if you fellows have some idea on it.

请让我知道,如果你的同胞们对此有一些想法。

Thanks !

谢谢 !

回答by Maadri

A simpler method can also be used, Try this:

也可以使用更简单的方法,试试这个:

$this->Form->create('Request',array('onsubmit'=>'return confirm("are you sure?");'))

回答by Daniel Wright

Update:Credit to bicyclefor 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.)

更新:感谢自行车固定我破碎的回调。详情请见下文。(对于 StackO 不允许答案作者对接受编辑有最终决定权,这是一个很大的问题。)



Here's a solution using jQuery:

这是使用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 */

回答by Paulo Margarido

I think your function is failing because you are never actually returning the value from confirmfrmSubmit().

我认为您的函数失败了,因为您实际上从未从 confirmfrmSubmit() 返回值。

Try adding the return part to the onSubmit field, like so:

尝试将返回部分添加到 onSubmit 字段,如下所示:

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

Your JS code should work as it is, the problem is just that the confirm value is never being passed to the form when the onSubmit handler is called.

您的 JS 代码应该可以正常工作,问题只是在调用 onSubmit 处理程序时,确认值永远不会传递给表单。

P.S. note that you can do the following in the JS code:

PS注意可以在JS代码中进行如下操作:

return confirm("Text");

Hope this helps.

希望这可以帮助。

回答by Simon Jensen

You were actually 99% of the way there in the beginning .. you just needed to add 'return' before your call to the validation function.

一开始,您实际上已经完成了 99% 的工作……您只需要在调用验证函数之前添加“返回”即可。

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

Will do it.

会做的。

回答by Aditya P Bhatt

I found a very simple solution to this query:

我为这个查询找到了一个非常简单的解决方案:

<?php echo $form->create('ModelName', array('type' => 'file', 'url' => 'url', 'name' => 'frmRegister', 'onsubmit' => 'validatefrm(); return false;')); ?>

<?php echo $form->create('ModelName', array('type' => 'file', 'url' => 'url', 'name' => 'frmRegister', 'onsubmit' => 'validatefrm(); return false;')); ?>

With onsubmitfunction, I simply defined 'validatefrm(); return false;'

使用onsubmit函数,我简单地定义了'validatefrm(); 返回假;'

function validatefrm(){ // my condition, when i needs it to submitted document.frmRegister.submit(); return true; }

function validatefrm(){ // my condition, when i needs it to submitted document.frmRegister.submit(); return true; }

and found it working smooch :)

并发现它工作顺利:)

Let me know, if it helps you.

让我知道,如果它对你有帮助。

回答by Sunil

There is the following step:-

有以下步骤:-

- include this jquery file-

- 包括这个 jquery 文件 -

echo $this->Html->css('/css/jquery-confirm.min.css', ['block' => 'css']);

Create submit button

创建提交按钮

<?= $this->Form->button('<i class="fa fa-check"></i><span class="hidden-xs"> ' . __('Save') . '</span>', ['type' => 'submit', 'escape' => false, 'id' => 'submit_form_id', 'class' => 'btn green', 'title' => 'Save']); ?>



$('body').on('click', '#submit_form_id', function (e) {
            e.preventDefault();
                  var submitFormVar=1;
            if (submitFormVar!= 0) {
                $.confirm({
                    title: '',
                    content: 'Do you want to stop submit form' ,
                    buttons: {
                        ok: function () {
                            $(form).submit();
                        },
                        cancel: function (o) {
                            return o;
                        }
                    }
                });
            } else {
                $(form).submit();
            }
        });

Jquery code

查询代码

  • if you want to submit the form then the value is 0
  • if you want to not submit the form then the value is 1
  • implement as your requirement logic
  • 如果要提交表单,则值为 0
  • 如果您不想提交表单,则值为 1
  • 实现为您的需求逻辑