Cakephp 2和sweetalert js没有提交表单数据

时间:2017-02-08 11:38:46

标签: javascript cakephp

我安装了javascript库sweetalert以将确认javascript原生函数替换为cakephp 2.5应用。

点击“是”按钮后,我的表单未提交数据。单击否按钮时,我有预期的行为,但未提交数据。

如果我选择是按钮,我如何使用甜蜜警报作为确认本机javascript函数,其中提交数据?

这是我的代码:

    <?php
//at default.ctp :
        echo $this->Html->css(array('sweetalert/sweetalert'));
        echo $this->fetch('css');
        echo $this->Html->script(array('sweetalert/sweetalert','sweetalert/sweetalert.min'));

    //at the form : 
    //echo $this->Form->submit('Save', array('div'=>true, 'name'=>'save', 'value' => true, 'onclick'=>'return confirm("Confirm data editing?");'));
    echo $this->Form->submit('Save', array('div'=>true, 'name'=>'save', 'value' => true, 'onclick'=>'return isConfirm()' ));
    echo $this->Form->end();
    ?>
    <script>
        swal({
              title: "Are you sure?",
              text: "you can always edit later!",
              type: "warning",
              showCancelButton: true,
              confirmButtonClass: "btn-danger",
              confirmButtonText: "Yes, edit the data!",
              cancelButtonText: "No, cancel please!",
              closeOnConfirm: false,
              closeOnCancel: false
            },
            function(isConfirm) {

              if (isConfirm) {
                    console.log(isConfirm);
                    swal("Edited!", "data has been edited.", "success");
              } else {
                    console.log(isConfirm);
                    swal("Cancelo", "data has not been edited :)", "error");
              }
        });
    </script>

2 个答案:

答案 0 :(得分:1)

尝试在onSubmit元素的form值中调用您的函数,并在用户取消时添加return false;

答案 1 :(得分:1)

这会覆盖确认javascript函数,使用正则表达式查找存储在html中的帖子值,并在确认后发布。取消时,它没有。它可以针对当前设置为删除的其他内容进行更改。

window.confirm = function(message) {

    console.log(message);
    console.log(this.document.activeElement.outerHTML);
    var html = this.document.activeElement.outerHTML;
    var finale = false;
    var returnVal = false;

    swal({

        title: 'Are you sure?',
        text: 'Are you sue you want to delete this item?',
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'

    }).then(function() {

        var post = html.match(/(document.post_)(.*)(submit\(\);)/g);
        eval(post[0])

    }, function(dismiss) {
        if (dismiss === 'cancel') {
            swal(
                'Cancelled',
                'File was not deleted',
                'error'
            )
        }
    })
};