在ajax调用中包含另一个php文件是有效的,但javascript文件不起作用;t负载


Including another php file inside ajax call works but javascript file doesn't load

我有一个表单向导php页面,在它的相关javascript(我是usingn prototype.js btw)中,我正在用Ajax.Updater更新一个div。真正有趣的是成功地包含了外部php文件,但javascript文件没有。这是我的代码:

wizard.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代码。

这里有一个很好的答案,你的问题

使用函数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;