Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从jquery对象中删除元素_Javascript_Php_Jquery - Fatal编程技术网

Javascript 如何从jquery对象中删除元素

Javascript 如何从jquery对象中删除元素,javascript,php,jquery,Javascript,Php,Jquery,我有一个特别的问题。在通过JSON发布元素数据之前,我想获取一些元素,遍历它,删除或编辑不需要的元素 我的函数是这样的 $("#submit_btn").off("click").on("click", function(e) { e.preventDefault(); pdfhtml = $("#Calculation").html(); var inputs = $("input[type=number]", pdfhtml);

我有一个特别的问题。在通过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);
});
$(“#提交”).关闭(“单击”).打开(“单击”,功能(e){
e、 预防默认值();
//创建一个临时dom结构,将目标html作为其内容
变量$temp=$(''{
html:$(“#计算”).html()
});
//修改临时dom结构
$temp.find(“输入[type=number]”。删除();
变量数据={};
//从临时对象获取更新html内容
数据[“数据”]=$temp.html();
警报(pdfhtml);
getdataJSON(“,”POST“,”html“,”数据,true);
});

jQuery不会这样修改字符串。所有
输入
都是
标记的内存表示,对这些标记的更改不会保存回
pdfhtml
字符串

您应该做的是克隆
#Calculation
div,然后执行您的操作:

$("#submit_btn").off("click").on("click", function(e) {
    e.preventDefault();

    var pdfhtml = $("#Calculation").clone(); // Clone this
    var inputs = pdfhtml.find("input[type=number]");

    $.each(inputs, function(k, v) {
        $(this).remove();
    });

    var data = {};
    data["data"] = pdfhtml.html(); // Read the HTML here...
    alert(pdfhtml);
    getdataJSON("","POST", "html", data, true); 
});

此外,如果我将警报放在其中,则每个警报都类似于此警报($(this.val());我总是对每个输入值保持警惕;)但是remove()不起作用。我不知道我还能如何操作文件。我需要去掉一些helper div和input元素。实际上我只需要值;)谢谢你的解释。我真的无法意识到我操作了错误的字符串。感谢您使用$.each和clone()提供的解决方案。我会用这个,因为它更适合我。不客气。别忘了,你真的不需要
$。每个
pdfhtml.find(“input[type=number]”。remove()
会很好:)我需要每个,因为我需要获取每个输入值并将其放在另一个div中,因为在PDF中我需要产品的数量。但是感谢您提供的信息,因为我需要删除一些helper div,而不需要保留任何数据;)