[英]Jquery ajax html replace
我的問題很簡單,我收到一個字符串和一個數字,但只顯示數字如何顯示兩者?
$.ajax({
type: "POST",
url: "data.php",
data: "act="+nr,
success: function (result) {
var arr = JSON.parse(result);
if ($.isArray(arr) == true) {
$.each(arr, function (i, n) {
$('#s_main #s_info').html("<p>+" + n + "</p>")
});
}
}
})
}); //ends here
我的PHP:
$act = $_POST['act'];
$output =array();
$act2 = "TXT!!!";
array_push($output,$act);
echo json_encode($output);
順便說一句,當我使用append而不是html時,結果是正確的,但是它將堆疊在上面,並且不會刪除以前的數據
您得到的是一個JSON 對象 。
if ($.isArray(arr) == true)
由於對象不是數組,因此此行不應返回true。 如果JSON.parse()
不起作用,它將返回一個等於false的值。 因此,您只需測試返回的對象是否成功即可。
var jsonObj = JSON.parse(result);
if (jsonObj) {
$.each(jsonObj, function (index, value) {
$('#s_main #s_info').html("<p>+" + value + "</p>")
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.