[英]Dirty form not working
我想监视表单的更改并在离开页面之前提醒用户。 所以我用一个肮脏的形式插件这里找到
这是我尝试的代码
形成
<?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>
现在,如果我尝试不保存就转到其他页面或链接,则会弹出弹出窗口。 但是,即使我尝试提交消息,也会出现弹出窗口。 为什么会这样?
但是,即使我尝试提交消息,也会出现弹出窗口。 为什么会这样?
脏表单尝试使用<input type="submit">
标记忽略提交的时间。 但是,使用JavaScript,也可以通过单击其他类型的表单标签(例如定位标签)来提交表单。
默认情况下,“脏表单”会将任何锚定标记单击解释为尝试从页面导航。 如果您正在使用一些将锚标记用于其他目的的JavaScript小部件,则可以通过将其放入ignoreSelector
,添加ignoreClass
或将它们放入带有ignoreClass
的容器元素中来忽略它们。 有关详细信息,请参见忽略事物 。
我知道了
<?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">
<div class="ignore">
<?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' ,'id'=>'update']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($){
// Enable on all forms
$('form').dirtyForms();
$('#update').click(function(){
$('.ignore').addClass($.DirtyForms.ignoreClass);
});
});
</script>
我检查是否单击了按钮。 如果是的话,那么我会以脏形式将ignore函数用于整个表单(位于div class =“ ignore”中),因此当我单击提交按钮时,将不会出现弹出窗口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.