關於ajax有關問題,先多謝各位了

來源:互聯網
上載者:User
關於ajax問題,線上等,先謝謝各位了
index.php頁面有兩個div,ID分別為div1,div2,對應的事件為:

$.ajax({
url: test.php,
type: 'post',
dataType:'text',
success: function (responseText) {
$('#div1').html(responseText);
$('#div2').html(responseText);
}
});

有沒有辦法將test.php頁面返回的值分別放到div1,div2呢?我想到的是將返回的值用js來切隔,然後放到裡面,還沒有實踐,不知行不行,如果可以,我覺得這樣好像不是很好。請問有其他的方法嗎?
------解決方案--------------------
完全可以,只是你要注意

responseText

裡面,不能包含你需要切割的那個字元。


推薦你用json,這樣比較方便些。就不會涉及切割了。


------解決方案--------------------
可以用 js 切割,但你得有唯一的切割標誌吧?
不然把本文也切割了,就不美了

返回多個資料一般用 json
$res = array(
'div1' => '相關內容',
'div2' => '相關內容',
)
echo json_encode($res);

$.ajax({
url: 'test.php',
type: 'post',
dataType:'json',
success: function (data) {
$('#div1').html(data.div1);
$('#div2').html(data.div2);
}
});

更一般的
$.post('test.php', {}, function(d) {
for(var i in d) $('#'+i).html(d[i]);
}, 'json');
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.