簡體   English   中英

在ajax調用中包含另一個php文件有效,但無法加載javascript文件

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

我有一個表單向導php頁面,並且在其相關的javascript中(我是usign 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM