2017-10-21 63 views
0

大家好, 我從HTML頁面獲得了一些值,用於打印表單和一些來自api的值,而打印按鈕點擊我打電話打印功能,在那個打印功能我調用過使用AJAX API值,與阿賈克斯值形式不打印bcoz API需要時間來加載數據庫,然後我寫了打印功能的AJAX的成功裏面這邊也沒有工作誰能請幫助我..返回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); 
     } 
      }); 

    } 
+0

你試過改變函數'myFunction(){};'爲'var myFunction = {}',然後將變量的值傳遞給AJAX?你忘了一些東西,指定了輸入到AJAX中的方法'type:「POST」' – Gabriel

回答

0

你看到成功的新窗口彈出?我認爲這裏有一個換行符這會導致運行時錯誤

 style=text- 

    align:center

而且,看起來不使用在成功處理程序中的「數據」的反應?在聲明所有綁定到print html的變量之前,您需要等待填充了'data'值的html。