0

问题:我所要做的就是使用他的 id 从数据库中获取用户的信息,然后将这些传入的信息打印在 pdf 文件或硬拷贝上。

尝试过的解决方案:PrintData(id)在代码中创建了一个如下所示的方法,它接受用户 ID 作为参数,然后使用 ajax 从数据库中获取与其相关的所有信息。然后将所有信息放入一个div中。i 使用打印方法打印该 div。不幸的是,该PrintElem()方法打印了 div 的先前内容(不是我们从与该 id 相关的服务器获取的当前内容)。我可以通过在 div 中添加另一个打印按钮来做到这一点,但我想在PrintData()方法将这些信息设置到该 div 时立即打印这些信息。这是我试图实现它的示例代码。如果您有更多信息,请随时问我。

function PrintData(id){
    var Data= "task=showuserdetail&id="+id;
    $.ajax({
        url:"taskprocess.php" ,
        data:Data,
        cache:false,
        dataType:'json',
        type:'POST',
        success: function(output){
            if(output[0] !=0){
                $('#viewDetail').show();     //main Div name
                $('#userID_Retrieve').html(id);
                $('#name').html(output[0]);
                $('#userName').html(output[1])
                $('#accountType').html(output[2]);
                $('#accountGroup').html(output[4]);
                $('#creationDate').html(output[6]);
                $('#streetAddress').html(output[7]);
                $('#state').html(output[8]);
                $('#city').html(output[9]);
                $('#birthDate').html(output[10]);
                $('#phoneNumber').html(output[11]);
            }
        },
        error:function (a, b , c){
            alert(a+" "+" "+c);
        }
    });
    PrintElem('#userDetail');
    return false;
}

其他方法

function PrintElem(elem)
{
    Popup($(elem).html());
}

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=400,width=600');
    mywindow.document.write('<html><head><title>Your details</title>');        
    mywindow.document.write('</head><body >');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');
    mywindow.print();
    mywindow.close();
    return true;
}
4

1 回答 1

0

我可能对您的问题有误,因为您的问题不是 100% 清楚......但是,我相信问题在于您对 $.ajax 异步调用的误解。

我相信你正在寻找以下发生

  1. 发出网络请求
  2. 收到响应时更新 div
  3. 使用新的 div 内容调用 PrintElem

但是,在您当前的代码中,当无法保证第 2 步已经完成时,您执行第 3 步。这意味着您实际上是在 PrintElem 更新之前调用它。

长话短说-确保您对 PrintElem 的调用在您的 $.ajax 成功中,以便它仅在更新 div 后调用 PrintElem

$.ajax({
    url:"taskprocess.php" ,
    data:Data,
    cache:false,
    dataType:'json',
    type:'POST',
    success: function(output){
        if(output[0] !=0){
            $('#viewDetail').show();     //main Div name
            $('#userID_Retrieve').html(id);
            $('#name').html(output[0]);
            $('#userName').html(output[1])
            $('#accountType').html(output[2]);
            $('#accountGroup').html(output[4]);
            $('#creationDate').html(output[6]);
            $('#streetAddress').html(output[7]);
            $('#state').html(output[8]);
            $('#city').html(output[9]);
            $('#birthDate').html(output[10]);
            $('#phoneNumber').html(output[11]);

            // now we can call this
            PrintElem('#userDetail');

        }
    },
    error:function (a, b , c){
        alert(a+" "+" "+c);
    }
});
于 2013-08-11T15:13:15.343 回答