在ajax调用中包含另一个php文件,但javascript文件无法加载

时间:2014-04-28 11:06:42

标签: javascript php ajax prototypejs

我有一个表单向导php页面及其相关的javascript(我使用原型.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;

1 个答案:

答案 0 :(得分:1)

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;