在将 html 主体作为数据发送到控制器之前从 ajax 中删除 html 元素

Removing html elements from html body in ajax before sending it as data to controller

我有一个 JSP 页面,其中包含两个按钮 "download" 和 "sendemail"。单击 Sendmail 按钮时,我调用了一个 ajax 方法,该方法生成 HTML 正文的 pdf 并将其发送到后端(控制器)。

我尝试使用:

document.getElementById('reportbuttons').remove();

之前:

doc.addHTML(document.body, function() {....

其中 "reportbuttons" 是包含“sendmail”和 "download" button.But 的 div 标签的 ID,一旦 "sendmail" 按钮出现,这两个按钮就会消失点击了。

function sendMail() { let doc = new jsPDF('p','pt','a4'); doc.setProperties({ title: ' Report PDF Document', subject: 'subject', author: 'XYZ', keywords: 'generated, javascript, web 2.0, ajax', creator: 'ABC ' }); doc.addHTML(document.body, function() { var data = doc.output('datauristring'); console.log(data); var reqJson = {}; reqJson.machineId = "<%=machineId%>"; reqJson.monthYear = "<%=monthYear%>"; reqJson.data = data; console.log(); $.ajax( { url : "sendMail/", type: "POST", dataType: 'json', data : JSON.stringify(reqJson), contentType: "application/json", success:function(data) { alert('mail sent successfully'); console.log(data); }, error: function(data) { } }); }); }

我想删除生成的pdf中的这两个按钮,但它应该不会在jsp网页上消失。

doc.addHTML(body,function(){}) 之前不要删除您的按钮,而是隐藏这两个按钮,并在您的 ajax 呼叫的 success/error 响应中再次显示这两个按钮。

                 success:function(data)
                 {
                     alert('mail sent successfully');
                     console.log(data);
                     document.getElementById('reportbuttons').style.display ="block";

                 }, 
                 error: function(data)
                 {
                     document.getElementById('reportbuttons').style.display ="block";
                 }

更新:

创建文档的克隆并从克隆中删除元素,然后使用 ajax 将更新后的克隆发送到控制器。

  var itm = document;
  var cln = itm.cloneNode(true);
  cln.getElementById("reportbuttons").remove();