Php yii2pjax在第二次表单提交时重新加载整个页面

Php yii2pjax在第二次表单提交时重新加载整个页面,php,yii,yii2,pjax,Php,Yii,Yii2,Pjax,我似乎不明白为什么pjax会在第二次表单提交时重新加载页面。它在从该url提取的第一次表单提交时完全按照预期工作,但是,在第一次提交后,它将丢失url的结尾。我检查了实际的html代码,表单保留了dataajax属性。当pjax重新加载整个页面时,我尝试增加超时时间,但这并没有改变任何事情 下面是我的观点中的代码 <?php Pjax::begin(['timeout' => 5000, 'id' => 'form-pjax']); ?> <?php $fo

我似乎不明白为什么pjax会在第二次表单提交时重新加载页面。它在从该url提取的第一次表单提交时完全按照预期工作,但是,在第一次提交后,它将丢失url的结尾。我检查了实际的html代码,表单保留了
dataajax
属性。当pjax重新加载整个页面时,我尝试增加超时时间,但这并没有改变任何事情

下面是我的观点中的代码

<?php Pjax::begin(['timeout' => 5000, 'id' => 'form-pjax']); ?>
    <?php $form = ActiveForm::begin([
        'method' => 'get',
        'action' => Url::to(['site/profile']),
        'options' => ['data-pjax' => true ],
    ]); ?>

        <?= $form->field($searchModel, 'searchstring', [
                'template' => '<div class="input-group">{input}<span class="input-group-btn">' .
                Html::submitButton('Search', ['class' => 'btn btn-default']) .
                '</span></div>',
            ])->textInput(['placeholder' => 'Find friends by username or email']);
        ?>

    <?php ActiveForm::end(); ?>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            'username',
            [
                'label' => 'View Profile',
                'format' => 'raw',
                'value'=>function ($data) {
                    return Html::a(Html::encode("View Profile"),'/site/redirectprofile/'.$data->id);
                },
            ], 
            [
                'label' => 'Follow',
                'format' => 'raw',
                'value'=>function ($data) {
                    return Html::a(Html::encode(Follow::find()->where(['user_id' => $data->id, 'follower_id' => Yii::$app->user->Id])->exists() ? 'Unfollow' : 'Follow'),'/site/follow/'.$data->id.'/'.$data->username);
                },
            ],                           
        ],
        'summary'=>'',
    ]); ?>
<?php Pjax::end(); ?>


每次pjax重新加载后,必须调用所需的javascript触发器/函数,如:

$('#form-pjax').on('pjax:success', function() {
  /*call Your functions here, or copy from your generated page js code,
    that bind event to pjax form, 
    at end of generated html You should have something like: */ 

 jQuery(document).on('submit', "#form-pjax form[data-pjax]", function (event) {
   jQuery.pjax.submit(event, '#form-pjax', {"push":true,"replace":false,"timeout":000,"scrollTo":false});
 });

});

这不是yii2的问题,而是javascript的工作方式,当您动态添加内容时,需要为每个添加的元素绑定触发器/事件。

您必须在每次pjax重新加载后调用所需的javascript触发器/函数,如:

$('#form-pjax').on('pjax:success', function() {
  /*call Your functions here, or copy from your generated page js code,
    that bind event to pjax form, 
    at end of generated html You should have something like: */ 

 jQuery(document).on('submit', "#form-pjax form[data-pjax]", function (event) {
   jQuery.pjax.submit(event, '#form-pjax', {"push":true,"replace":false,"timeout":000,"scrollTo":false});
 });

});

这不是yii2的问题,而是javascript的工作方式,当您动态添加内容时,它需要为每个添加的元素绑定触发器/事件。

其中到底包含哪些代码,我目前没有任何代码支持PJAX,但这些代码不起作用,它具有相同的行为。pjax在第一次搜索时工作,但在第二次搜索时重新加载页面。什么代码会进入其中,我目前没有任何代码启用pjax。如果pjax不工作,它具有相同的行为。pjax在第一次搜索时工作,但在第二次搜索时重新加载页面