如何使用 ajax post span 的 ID

How to post ID of span using ajax

我需要 post 使用 ajax 的跨度 ID,但它不起作用 :D alert($(this).attr("id")) 有效,为什么不起作用 ajax?

$(".nav-item").click(function() {
    $.ajax({
        method: "POST",
        url: "load.php",
        data: {
            page: $(this).attr("id");
        }
    }).done(function(result) {
        $("#main").html(result);
    });
});

如果那是您的代码的精确副本,请删除页面后的分号:

data: {
    page: $(this).attr("id"); <---- remove this semicolon
 }

...否则,您的代码是正确的,这是一个示例(检查控制台 > 网络请求 > 请求发送的有效负载): https://jsfiddle.net/co6yupzg/