我有一个表单向导php页面及其相关的javascript(顺便说一句,我正在使用prototype.js),我正在使用Ajax.Updater更新一个div 。真正有趣的是外部 php 文件已成功包含,但 javascript 文件未包含。这是我的代码:

向导.php

<div id="step1" class="tab-pane active"> 
    <h3 class="block"><?=$core->l("report_selection")?></h3> 
    <div class="control-group"> 
        <label class="control-label"><?=$core->l("report_name")?> 
            <span class="required">*</span> 
        </label> 
        <div class="controls"> 
            <select id="report_selection" field="report_selection" class="m-wrap span10" appEditor="true"> 
                <?=combo_creator::render_reports_by_authorization();?> 
            </select> 
        </div> 
    </div> 
</div> 
<div id="step2" class="tab-pane"> 
    <!-- Here I show external php file I include in ajax call--> 
</div> 

wizard.js

reportSelectionChanged: function() { 
    switch (this.reportSelection.getValue()) { 
        case A: 
            new Ajax.Updater( 
                    'step2', get_report_type.ajax.php,  
                    { 
                        parameters : {  
                            reportSelection:this.reportSelection.getValue() 
                        }, 
                        onComplete : function(){         
                            this.reportLastConsumptionFilter    = new ReportLastConsumptionFilter(this); 
                        } 
                    }); 
            break; 

在ajax里面是get_report_type.ajax.php

switch ($report_selection) { 
     case A: 
        $include_page = "last_cons_report.php"; 
        break; 
} 
if (isset($include_page)) { 
    echo include $include_page; 
} 

last_cons_report.php

<!-- Some HTML elements --> 
 ... 
 
    <script type="text/javascript" src="scripts/reportLastConsumptionFilter.js"></script> 

一切都很好,但正如你所看到的,我添加了一个 JavaScript。问题是我的主文件似乎不包含这个 javascript 文件,其他都很好。

你能看出这里的问题吗?

编辑:解决方案

reportSelectionChanged: function() { 
    switch (this.reportSelection.getValue()) { 
        case A: 
            new Ajax.Updater( 
                    'step2', get_report_type.ajax.php,  
                    { 
                        parameters : {  
                            reportSelection:this.reportSelection.getValue() 
                        }, 
                        onComplete : jQuery.getScript("scripts/reporting/aircomConsumption/reportAircomConsumptionFilterSorting.js", function() { 
                                alert("script loaded and executed"); 
                            }); 
                        } 
                    }); 
            break; 

请您参考如下方法:

Ajax 未加载 <script>你的文件的标签,这就是问题所在,这一行只是被ajax忽略了。考虑使用 JavaScript 函数来导入您的 JavaScript 代码。

这里有一个good answer to your problem

使用函数loadScript(url, callback) ,在您的代码中:

reportSelectionChanged: function() { 
switch (this.reportSelection.getValue()) { 
    case A: 
        new Ajax.Updater( 
                'step2', get_report_type.ajax.php,  
                { 
                    parameters : {  
                        reportSelection:this.reportSelection.getValue() 
                    }, 
                    onComplete : function(){  
                        loadScript("scripts/reportLastConsumptionFilter.js",function(){ 
                            // callback code here. 
                        }) 
                        this.reportLastConsumptionFilter    = new ReportLastConsumptionFilter(this); 
                    } 
                }); 
        break; 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!