簡體   English   中英

從AJAX php文件調用javascript函數不起作用

[英]call javascript function from AJAX php file isn't working

解決了! 我只用JavaScript來完成任務。 現在,它從php讀取文本並使用坐標。

        var texto = HttpReq.responseText;
        var lats,lons;
        var pos = texto.search(' ');
        lats = texto.substring(0,pos-1);
        lons = texto.substring(pos+1);

謝謝你們! 你太棒了!


我的網站有一個主頁,在用戶選擇城市后,我要在Google地圖上顯示它。 城市的坐標在表格中,因此我在單獨的.php文件中使用php,可通過AJAX訪問。 在單獨的.php中,我想從主頁調用JS函數以將點添加到地圖(JS地圖對象位於主頁上,我也嘗試將所有JS放在.js文件中,但是該函數仍然根本沒有被調用(我正在使用FireBug對其進行斷點操作,但從未調用該函數)。

AJAX的GET結果是這樣的:

Latitude: -21.15361<BR>Longitude: -41.567501<BR><script src="functions.js" type="text/javascript">addPoint(-21.15361,-41.567501);</script>

緯度和經度出現在屏幕上,但未調用方法addPoint。 我在這里想念一些愚蠢的東西嗎? 提前致謝。

-編輯-好的,這就是我想要做的(位於getcoords.php中,該文件名為ajax):

$stmt = $dbh->prepare("select id_mun,lat,lon from municipio where id_mun = '".$_GET['mun']."'");
if ($stmt->execute()) {
    echo '<script src="functions.js" type="text/javascript"></script>';
    while ($row = $stmt->fetch(PDO::FETCH_BOTH)) {
        echo "Latitude: $row[1]<BR>";
        echo "Longitude: $row[2]<BR>";
        echo '<script type="text/javascript">';
        echo 'alert("testando");';
        echo "addPoint($row[1],$row[2]);";
        echo "</script>";
    }
}

和從我的div轉到innerHtml的結果(並且不執行):

<script src="functions.js" type="text/javascript"></script>Latitude: -15.779722<BR>Longitude: -47.929722<BR><script type="text/javascript">alert("testando");addPoint(-15.779722,-47.929722);</script>

再次感謝。

如果存在src屬性,則不會執行<script></script>任何代碼。 您將需要兩個腳本塊而不是一個:

<script src="functions.js" type="text/javascript"></script><script type="text/javascript">addPoint(-21.15361,-41.567501);</script>

希望有幫助!

<script>中的內容僅在頁面加載時執行。 您需要從AJAX響應處理程序中調用代碼,而不僅僅是將其注入到頁面中。

暫無
暫無

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

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