我想在离开页面之前监控表单的变化并提醒用户。所以我使用了一个脏表单插件在这里找到
这是我试过的代码
形式
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Countries;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $model app\models\Organizations */
/* @var $form yii\widgets\ActiveForm */
?>
<link rel="stylesheet" href="<?php echo Yii::$app->request->baseUrl;?>/css/userHome.css">
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery.dirtyforms/2.0.0-beta00005/jquery.dirtyforms.min.js"></script>
<div class="organizations-form">
<h2>Update Billing Address</h2><br>
<div class="col-md-10 col-md-offset2">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'bill_name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'bill_address')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'bill_country_id')->dropDownList(ArrayHelper::map(Countries::find()
->all(), 'id', 'name'),['prompt'=>'Choose a Country'])->label('Select Country') ?>
<?= $form->field($model, 'bill_state')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'bill_city')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'bill_postal')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'bill_mobile')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-primary' : 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
使用的脚本
<script>
jQuery(document).ready(function($){
// Enable on all forms
$('form').dirtyForms();
});
</script>
现在,如果我尝试在不保存的情况下转到其他页面或链接,我会弹出消息。但是当我尝试提交消息时,即使出现弹出窗口。为什么呢?