get hidden row content from datatable and submit it along with the form

随声附和 提交于 2019-12-03 20:09:45
$("#form").on("submit",function(){
    if($("#form").valid()){

       //Loop through the TR records
       oTable.$("tr").each(function(index, nRow){
            //Select the input from the row
            //var rowInput = $("input", nRow);
            //Select the text area from the row
            //var rowTextarea = $("textarea", nRow);

            //Add to form
            var nHidden = document.createElement( 'input' );
            nHidden.type = 'hidden';
            nHidden.name = "hidden_input_"+index;

            //Assuming there is one input per row
            nHidden.value = $("input", nRow).val();
            //alert(nHidden.value);
            $("#form").append( nHidden );
        });

        $("#form").submit();         
    }else {
        validator.focusInvalid();
        return false; 
    }    
});

Alright.! Time to wrap up. Thanks to @Bret for giving me a direction. Below is the code snippet that's finally working -

      $("#form").on("submit",function(){
        if($("#form").valid()){

            var nNodes = oTable.fnGetHiddenTrNodes();

                    $('td', nNodes).each(function(index,ncolumn) {

                        var nHidden = document.createElement( 'input' );
                        nHidden.type = 'hidden';
                        nHidden.name = $("input", ncolumn).attr("name");
                        nHidden.value = $("input", ncolumn).val();

                        if(typeof(nHidden.name)  != "undefined")
                        $("#form").append( nHidden );

                        nHidden = document.createElement( 'input' );
                        nHidden.type = 'hidden';
                        nHidden.name = $("textarea", ncolumn).attr("name");
                        nHidden.value = $("textarea", ncolumn).val();

                        if(typeof(nHidden.name)  != "undefined")
                        $("#form").append( nHidden );

                        nHidden = document.createElement( 'input' );
                        nHidden.type = 'hidden';
                        nHidden.name = $("textarea", ncolumn).attr("name");
                        nHidden.value = $("textarea", ncolumn).val();

                        if(typeof(nHidden.name)  != "undefined")
                        $("#form").append( nHidden );

                        nHidden = document.createElement( 'input' );
                        nHidden.type = 'hidden';
                        nHidden.name = $("select", ncolumn).attr("name");
                        nHidden.value = $("select", ncolumn).attr("value");

                        if(typeof(nHidden.name)  != "undefined")
                        $("#form").append( nHidden );

                    });

            clickedSave = true;


        }else {
            validator.focusInvalid();
            return false;
        }

    });  

Thanks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!