Jquery ajax html replace

Jquery ajax html replace

本文关键字:replace html ajax Jquery      更新时间:2023-09-26

我的问题很简单我收到字符串和一个数字,但只显示数字如何同时显示两者?

     $.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>")
  });
}