2010-05-24 46 views
1

早上/下午夥計。PHP/JQuery/AJAX,很少出問題

寫一些JQuery AJAX shizz,並有點卡住。我已經完成了調用php文件的實際過程,它只是試圖讓頁面上的html以我想要的方式進行更改。我想擺脫ajax調用等使用的id,並將其替換爲從PHP文件傳遞的HTML。代碼如下...

$(".save_places").click(function() { 
    $.ajax({ 
    url: "{/literal}{$sRootPath}{literal}system/ajax/fan_bus.php", 
    type: "POST", 
    data: ({id : this.getAttribute('id')}), 
    dataType: "html", 
    success: function(msg){ 
     $(this).before(msg); 
     $(this).empty(); 
     alert(msg); 
    } 
    }); 
    return false; 
}); 

而且HTML很簡單;

<p class="links"> 
    <a href="#" class="save_places" id="bus_{$businesses.results[bus].id}_{$sMemberDetails.id}"><img src="{$sThemePath}images/save_places.png" alt="Save to My Places" /></a> 
    <a href="#"><img src="{$sThemePath}images/send_friend.png" alt="Send to a Friend" /></a> 
</p> 

所有中籤功能的東西的代碼實驗糖化,任何幫助嗎?

一如既往的感謝。

回答

3

我認爲你是什麼後.replaceWith(),這樣的:

$(this).replaceWith(msg); 

這與內容回來在msg替換<a></a>

另外,如果你確定元素具有的ID,你可以這樣做:

data: {id : this.id}, 
+0

尼克,精緻的數據:東西的作品一種享受,更整潔,謝謝!雖然replaceWith似乎沒有工作,但標記沒有變化,也沒有javascript錯誤:/當使用$(this)時,其他class =「save_places」的加載會不會影響它? – Tom 2010-05-24 11:57:01

+0

@thebluefox - 我還沒有完全喚醒:)給這個試試你的成功成功:$ .proxy(function(msg){$(this).replaceWith(msg);},this)' – 2010-05-24 12:02:09

+0

Boom我們走吧!絕對的配偶,非常感謝。來吧!我已經6個小時了! – Tom 2010-05-24 12:26:59

1
linksPara.replaceChild(newElements, oldAtag);