肮脏的形式不起作用
Dirty form not working
我想监控表单的变化并在离开页面之前提醒用户。所以我使用了一个脏表单插件 found here
这是我试过的代码
形式
<?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>
现在,如果我尝试转到其他页面或 link 而不保存,我会收到弹出消息。但是,当我尝试提交消息时,即使出现弹出窗口。
为什么会这样?
我成功了
<?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>
我检查了按钮是否被点击。如果是,那么我对整个表单(在 div class="ignore" 中)使用脏形式的忽略功能,所以当我单击按钮提交时,不会有弹出窗口。
But when i try to submit message even then the popup is appearing. why is that so?
Dirty Forms 试图通过使用 <input type="submit">
标签忽略提交。但是,使用 JavaScript 也可以通过单击其他类型的表单标签(例如锚标签)来提交表单。
默认情况下,脏表单会将任何锚标记点击解释为尝试从页面导航。如果您使用的某些 JavaScript 小部件将锚标记用于其他目的,您可以通过将它们放在 ignoreSelector
、添加 ignoreClass
或将它们放在容器元素内来忽略它们与ignoreClass
。有关详细信息,请参阅 ignoring things。
我想监控表单的变化并在离开页面之前提醒用户。所以我使用了一个脏表单插件 found here
这是我试过的代码
形式
<?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>
现在,如果我尝试转到其他页面或 link 而不保存,我会收到弹出消息。但是,当我尝试提交消息时,即使出现弹出窗口。 为什么会这样?
我成功了
<?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>
我检查了按钮是否被点击。如果是,那么我对整个表单(在 div class="ignore" 中)使用脏形式的忽略功能,所以当我单击按钮提交时,不会有弹出窗口。
But when i try to submit message even then the popup is appearing. why is that so?
Dirty Forms 试图通过使用 <input type="submit">
标签忽略提交。但是,使用 JavaScript 也可以通过单击其他类型的表单标签(例如锚标签)来提交表单。
默认情况下,脏表单会将任何锚标记点击解释为尝试从页面导航。如果您使用的某些 JavaScript 小部件将锚标记用于其他目的,您可以通过将它们放在 ignoreSelector
、添加 ignoreClass
或将它们放在容器元素内来忽略它们与ignoreClass
。有关详细信息,请参阅 ignoring things。