Php yii2pjax只是第一次工作

Php yii2pjax只是第一次工作,php,yii2,pjax,Php,Yii2,Pjax,我正在将pjax与Yi2一起使用,我在pjax方面遇到了一个问题。 所有脚本仅在第一次和发送pjax请求并更新表内容后正常工作,脚本和pjax不工作。如果我删除行$.pjax.reload({container:'#products table'})所有脚本都在工作。我还发现了这个解决方案():“$(document).on('ready pjax:success',function(){//to do});”,它工作得很好,但在添加更多jquery代码后,它又停止了工作。请如果有人有相同的问题

我正在将pjax与Yi2一起使用,我在pjax方面遇到了一个问题。 所有脚本仅在第一次和发送pjax请求并更新表内容后正常工作,脚本和pjax不工作。如果我删除行
$.pjax.reload({container:'#products table'})所有脚本都在工作。我还发现了这个解决方案():“
$(document).on('ready pjax:success',function(){//to do});
”,它工作得很好,但在添加更多jquery代码后,它又停止了工作。请如果有人有相同的问题,请分享解决方案。谢谢

$(document).on('ready pjax:success', function(){

    $(".product-edit-submit").on('click', function(){
            $.ajax({
                type : "POST",
                url : editUrl,
                data: {
                    id: this.id,
                    productName: productName,
                    productCost: productCost,
                    _csrf: csfr
                },
                success  : function(response) {
                    $.pjax.reload({container:'#products-table'});
                },
                error : function(response){
                    console.log(response);
                }
            });
            return false;

        });
    });
视图:

.......

如果您的代码在添加额外库后停止,请尝试在视图中注册它,以确保在包含所有库后执行:

<?php
$js = <<< 'JAVASCRIPT'
$(document).on('ready pjax:success', function(){

$(".product-edit-submit").on('click', function(){
        $.ajax({
            type : "POST",
            url : editUrl,
            data: {
                id: this.id,
                productName: productName,
                productCost: productCost,
                _csrf: csfr
            },
            success  : function(response) {
                $.pjax.reload({container:'#products-table'});
            },
            error : function(response){
                console.log(response);
            }
        });
        return false;

    });
});
JAVASCRIPT;
$this->registerJs($js,\yii\web\View::POS_READY);

我找到了第二次脚本未触发的原因。

我将脚本放在
$(document).on('ready pjax:success',function(){//code})之外。将其放入
pjax:success
之后,所有脚本都再次工作。谢谢

只需更改此行
$(“.product edit submit”)。在('click',function(){

$(document).on('ready pjax:success', function(){

    $(".product-edit-submit").on('click', function(){
            $.ajax({
                type : "POST",
                url : editUrl,
                data: {
                    id: this.id,
                    productName: productName,
                    productCost: productCost,
                    _csrf: csfr
                },
                success  : function(response) {
                    $.pjax.reload({container:'#products-table'});
                },
                error : function(response){
                    console.log(response);
                }
            });
            return false;

        });
    });
至:

 $(document).on('click', '.product-edit-submit', function () {

然后它会工作

感谢您的响应@mmta41,但这不起作用。只有在发送pjax请求并重新加载表容器之后,问题才会出现。如果我删除$.pjax.reload({container:'#products table'));所有脚本都在工作,但表内容没有更新。我必须刷新页面才能看到更改。请发表评论。