jQuery:点击 Ajax return 数据但在第一次调用时显示在警报中

jQuery: On Click Ajax return data but does show in alert on first call

我有一些 jquery 代码。当单击按钮时,它应该向服务器控制器操作发送 ajax 调用,从那里获取一些数据并将其 return 放入 ID 'note-popover' 的 'DIV' 中。

.on('click.notes', '.pd-notes', function (e) {
        e.preventDefault();
        $.post("a_a/pd_notes", function (data) {
            $("#note-popover").html(data);
        });
        alert("Data Returned" + $("#note-popover").html());
    });

我也在 Chrome 开发工具中测试了这段代码,在第一次点击时,它会调用服务器,但是警报内容显示为空,即使在 DOM 'DIV' 填充了 returned 数据,但是每次连续调用警报似乎都是 return 警报中的数据。

我是新来的,所以这可能是一个愚蠢的问题。这种行为的原因是什么以及如何解决它。

感谢任何帮助

谢谢

数据仅在处理程序内部有效

.on('click.notes', '.pd-notes', function (e) {
        e.preventDefault();
        $.post("a_a/pd_notes", function (data) {
            $("#note-popover").html(data);
            alert("Data Returned" + $("#note-popover").html());
        });
    });

$.post 是异步的并开始操作,在您的示例中 returns 立即到您的 alert

稍后数据返回到您提供给post的回调函数。