从datatable中获取隐藏的行内容,并将其与表单一起提交

编程入门 行业动态 更新时间:2024-10-25 18:23:59
本文介绍了从datatable中获取隐藏的行内容,并将其与表单一起提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我遇到了一个问题,现在吃了我的时间。我有一个有7个记录的表(例如),这个表有输入和textarea元素,用户可以在其中键入。现在分页的值为每页5个记录,我有两个页面。用户将数据输入评论部分,点击分页中的下一步,并在评论部分输入值。从技术上讲,用户可以通过数据分页逻辑分割两个页面的值。问题是,当用户点击保存时,它只保存那些在焦点上的元素,更容易理解,这些在页面上是可见的。我阅读了API和常见问题解答,并清楚地表明datatable隐藏了不重点的元素,使得它们几乎不可能在DOM中找到。以下是代码,我需要帮助我如何使用fnGetHiddenTrNodes()方法从隐藏的行获取数据,创建隐藏的元素,并在提交表单之前将它们附加到现有的可见表单元素中。我尝试了以下代码,但它不工作。 on(submit,function(){ if($($($) $ #form)。valid()){ var nNodes = oTable.fnGetHiddenTrNodes(); for(var i = 0; i< nNodes.length; i ++) { var nHidden = document.createElement('input'); nHidden.type ='hidden'; nHidden.name =hidden_​​input _+ i; nHidden。 value = $('input',nNodes).val(); //alert(nHidden.value); this.appendChild(nHidden); } $(#form)。submit(); } else { validator.focusInvalid(); return false; } });

非常感谢任何帮助。

解决方案

好的。时间结束感谢@Bret给我一个方向。以下是最终工作的代码片段 -

$(#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.valu e = $(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(); 返回false; } });

谢谢。

I'm stuck with an issue and its eating my time now. I have a table with 7 records (for instance) and this table has input and textarea elements, where a user can type in. Now with pagination having the value as 5 records per page, I have two pages. The user enters data into comments section, clicks on the "next" in the pagination and enters value in the comments section. So technically, the user entered values into both the pages as splitted by datatable pagination logic. The problem is, when the user hits save, its saving only those elements which are on focus, more easy to understand which are visible on the page. I read the API and FAQs, and its clear that datatable hides the elements which are not in focus, making them practically impossible to find in DOM. Below is the code and I need help on how can I get the data from the hidden rows using the fnGetHiddenTrNodes() method, create hidden elements and append them to the existing visible table elements before submitting the form. I tried the below code, but it isn't working.

$("#form").on("submit",function(){ if($("#form").valid()){ var nNodes = oTable.fnGetHiddenTrNodes(); for ( var i=0 ; i<nNodes.length ; i++ ) { var nHidden = document.createElement( 'input' ); nHidden.type = 'hidden'; nHidden.name = "hidden_input_"+i; nHidden.value = $('input', nNodes).val(); //alert(nHidden.value); this.appendChild( nHidden ); } $("#form").submit(); }else { validator.focusInvalid(); return false; } });

Any help is greatly appreciated.

解决方案

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.

更多推荐

从datatable中获取隐藏的行内容,并将其与表单一起提交

本文发布于:2023-10-09 18:05:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476406.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   内容   并将其   datatable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!