简体   繁体   English

脏表格不起作用

[英]Dirty form not working

i wanted to monitor the forms for change and alerts the user before leaving the page. 我想监视表单的更改并在离开页面之前提醒用户。 so i used a dirty form plugin found here 所以我用一个肮脏的形式插件这里找到

this is the code i tried 这是我尝试的代码

form 形成

  <?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 used 使用的脚本

<script>
jQuery(document).ready(function($){ 

    // Enable on all forms
    $('form').dirtyForms();
});
</script>   

now if i try to go to other pages or link without saving i get popup meassege. 现在,如果我尝试不保存就转到其他页面或链接,则会弹出弹出窗口。 But when i try to submit message even then the popup is appearing. 但是,即使我尝试提交消息,也会出现弹出窗口。 why is that so? 为什么会这样?

But when i try to submit message even then the popup is appearing. 但是,即使我尝试提交消息,也会出现弹出窗口。 why is that so? 为什么会这样?

Dirty Forms attempts to ignore when a submit happens by using an <input type="submit"> tag. 脏表单尝试使用<input type="submit">标记忽略提交的时间。 However, with JavaScript it is also possible to submit a form by clicking other types of form tags (such as anchor tags). 但是,使用JavaScript,也可以通过单击其他类型的表单标签(例如定位标签)来提交表单。

Dirty Forms by default will interpret any anchor tag click as an attempt to navigate from the page. 默认情况下,“脏表单”会将任何锚定标记单击解释为尝试从页面导航。 If you are using some JavaScript widget that uses anchor tags for other purposes, you can ignore them by putting them in the ignoreSelector , adding the ignoreClass , or putting them inside of a container element with the ignoreClass . 如果您正在使用一些将锚标记用于其他目的的JavaScript小部件,则可以通过将其放入ignoreSelector ,添加ignoreClass或将它们放入带有ignoreClass的容器元素中来忽略它们。 See ignoring things for details. 有关详细信息,请参见忽略事物

i got it 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">
        <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>   

i checked whether the button is clicked. 我检查是否单击了按钮。 if yes then i used the ignore function in dirty form to the whole form(which is in div class="ignore") so when i click the button to submit there will be no popup's. 如果是的话,那么我会以脏形式将ignore函数用于整个表单(位于div class =“ ignore”中),因此当我单击提交按钮时,将不会出现弹出窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM