我有一个具体问题。我想抓取一些元素,遍历它并删除或编辑不需要的元素,然后再通过 JSON 发布该元素数据。
我的函数如下所示。
$("#submit_btn").off("click").on("click", function(e) {
e.preventDefault();
pdfhtml = $("#Calculation").html();
var inputs = $("input[type=number]", pdfhtml);
$.each(inputs, function(k, v) {
$(this).remove();
//$(this, pdfhtml).remove(); //also not working
})
var data = {};
data["data"] = pdfhtml;
alert(pdfhtml);
getdataJSON("","POST", "html", data, true);
});
我遇到的问题是没有任何反应,在我提交此数据后元素仍然存在。我究竟做错了什么?我需要这个才能使用 mpdf6 (PHP) 生成 pdf。
最佳答案
您正在创建一个临时 jQuery 对象并删除输入表单,但原始字符串 pdfhtml
未修改
$("#submit_btn").off("click").on("click", function (e) {
e.preventDefault();
//create a temp dom structure with the target html as its content
var $temp = $('<div />', {
html: $("#Calculation").html()
});
//modify the temp dom structure
$temp.find("input[type=number]").remove();
var data = {};
//get the update html content from the temp object
data["data"] = $temp.html();
alert(pdfhtml);
getdataJSON("", "POST", "html", data, true);
});
关于javascript - 如何从jquery对象中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29962280/