返回Ajax成功的另一个功能

时间:2017-10-21 14:55:33

标签: jquery html ajax

大家好,              我从html页面获取了一些值来打印表单和来自api的一些值,而打印按钮点击我正在调用print函数,在那个打印函数中我也使用AJAX调用API值,ajax值表单不打印bcoz api花时间加载数据库,然后我在ajax里面写了打印功能,这里也没有用,任何人都可以帮帮我..

        function myFunction() {

       var jobcard_id=document.getElementById('jobcard_id').value;
       var vehicle_num1=document.getElementById('veh-numbr1').value;
       var vehicle_num2=document.getElementById('veh-numbr2').value;
       var vehicle_num3=document.getElementById('veh-numbr3').value;
       var vehicle_num4=document.getElementById('veh-numbr4').value;
       var vehicle_number=vehicle_num1+" "+vehicle_num2+" "+vehicle_num3+" "+vehicle_num4;
       var cust_name=document.getElementById('cust_name').value;
       var cont_num=document.getElementById('cont_num').value;
       var cont_addr=document.getElementById('cont_addr').value;
       var brand=document.getElementById('brand').value;
       var model=document.getElementById('model').value;
       var kms=document.getElementById('kms').value;
       var toolkit_availability=document.getElementById('toolkit_availability').value;
       if(toolkit_availability == "True"){ var tol_kit= 'Yes';}else{var tol_kit='No';}           
       var fuel_percentage=document.getElementById('fuel_percentage').value;
       //var customer_complaint=document.getElementById('customer-complaint').value;
       var serviceType=document.getElementById('serviceType').value;
       var delivery_time=document.getElementById('delivery-time').value;
       var mechanic_name=document.getElementById('mechanic_name').value;
       var total_estimation=document.getElementById('total-estimation').value;

        $.ajax({
    url: "/dealer/details",
     dataType : 'json',
     success: function(data) {
       //called when successful
   //alert(data[0].sunday);
           alert(data.address);
         var printWindow = window.open('', '', 'height=400,width=800');
        printWindow.document.write('<html><head><title>Jobcard</title>');
        printWindow.document.write('</head><body ><br><h3 style="text-
      align:center;"></h3><br><hr><br>');
        printWindow.document.write('<h4 style="text-align:center;">Invoice Number: '+jobcard_id+'</h4>');
        printWindow.document.write("<table>");
        printWindow.document.write("<tr><td style='width:300px;'><b><h2>Customer Details:</h2></b></td><td style='width:300px;'></td><td style='width:300px;'><b><h2>Vehicle Details:</h2></b></td></tr>");
        printWindow.document.write("<tr><td style='width:300px;'>Vehicle Number: "+vehicle_number+"</td><td style='width:300px;'></td><td style='width:300px;'>Brand : "+brand+"</td></tr>");
        printWindow.document.write("<tr><td style='width:300px;'>Name: "+cust_name+"</td><td style='width:300px;'></td><td style='width:300px;'>Model : "+brand+"</h2></td></tr>");
        printWindow.document.write("<tr><td style='width:300px;'>Contact Number: "+cont_num+"</td><td style='width:300px;'></td><td style='width:300px;'>Kilometers: "+kms+"</td></tr>");
        printWindow.document.write("<tr><td style='width:300px;'>Address: "+cont_addr+"</td><td style='width:300px;'></td><td style='width:300px;'>Toolkit: "+tol_kit+"</td></tr>");
        printWindow.document.write("<tr><td style='width:300px;'></td><td style='width:300px;'></td><td style='width:300px;'>Fuel Percentage: "+fuel_percentage+"</td></tr>");
        printWindow.document.write("<tr><td><br><br></td></tr>");
        printWindow.document.write("</table><br><table cellpadding='20' cellspacing='20' border='1' style='border-collapse:collapse;'>");
        printWindow.document.write("<tr><th>SERIVICE TYPE</th><th>CUSTOMER COMPLAINTS</th><th>MECHANIC NAME</th></tr>");        
        printWindow.document.write("<tr><td>"+serviceType+"</td><td>"+customer_complaint+"</td><td>"+mechanic_name+"</td></tr>");
        printWindow.document.write("</table><br><br><br>Estimated Delivery Time: "+delivery_time+"<br><br>Estimated Price: "+total_estimation+" INR<br><br><br><br><br><Br><hr>");
        printWindow.document.write('</body></html>');
        printWindow.document.close();
        printWindow.print();
  },
     error: function(e) {
//called when there is an error
console.log(e.message);
         }
             });

   }

1 个答案:

答案 0 :(得分:0)

您是否看到成功的新窗口弹出窗口?我认为这里有一个换行符导致运行时错误

 style=text-

   align:center

另外,看起来您没有使用&#39;数据&#39;成功处理程序中的响应?你需要等待填充了数据的html&#39;声明在print html中绑定的所有变量之前的值。