在 yii2 中确认删除按钮

delete button with confirmation in yii2

此 view.php 是在 Yii2 中使用 CRUD 生成的,但删除按钮未确认 window。

但是在 Yii2 中使用 CRUD 生成的 index.php 中,删除确认 window 有效。

<p>
    <?= Html::a('Edit', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Delete', ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
    ]) ?>
    <?= Html::a('Back','index', ['class' => 'btn btn-warning']) ?>
</p>

删除 index.php 中的按钮:

<a href="var/delete?id=2" title="Delete" aria-label="Delete" data-pjax="0" data-confirm="Are you sure you want to delete this item?" data-method="post"><span class="glyphicon glyphicon-trash"></span></a>

删除 view.php 中的按钮:

<a class="btn btn-danger" href="var/delete?id=2" data-confirm="Are you sure you want to delete this item?" data-method="post">Delete</a>

section about yii.js 仍在进行中。

要获得确认的默认支持,您应该注册 yii.js

使用yii\web\YiiAsset

use yii\web\YiiAsset;
YiiAsset::register($this);

registering script files(不推荐,仅举个例子)

$this->registerJsFile('@yii/assets/yii.js', ['depends' => [\yii\web\JqueryAsset::className()]]);