Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.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
Jquery 检索div元素时tr标记丢失_Jquery_Html - Fatal编程技术网

Jquery 检索div元素时tr标记丢失

Jquery 检索div元素时tr标记丢失,jquery,html,Jquery,Html,我有一个AJAXPOST方法,我正在使用来自FreeMarker模板(addNewRow.ftl)的响应html。响应html由两个div元素组成——一个包含表行数据,另一个包含验证错误 将表格行追加到表体时,tr标记丢失-导致显示不正确。我目前正试图通过一个虚拟表标记传递数据,看看这是否解决了问题。在此期间,我希望得到任何指导 html代码 <table> <tbody> <tr> <td><input valu

我有一个AJAXPOST方法,我正在使用来自FreeMarker模板(addNewRow.ftl)的响应html。响应html由两个div元素组成——一个包含表行数据,另一个包含验证错误

将表格行追加到表体时,tr标记丢失-导致显示不正确。我目前正试图通过一个虚拟表标记传递数据,看看这是否解决了问题。在此期间,我希望得到任何指导

html代码

<table>
    <tbody>
    <tr>
      <td><input value="R1C1"></td>
      <td><input value="R1C2"></td>
    </tr>
    </tbody>
</table>

js代码

jQuery(function ($) {
    //to simulate the ajax request
    var html = '<div id="mainDiv"><tr>\
   <td>\<input value="R2C1">\</td>\
   <td>\<input value="R2C2">\</td>\
</tr></div><div id="errDiv">error Test</div>';

    var obj = $('table')
    $.post('/echo/html/', {
        html: html
    }, function (template) {
        console.log(template);
        var mainDiv = $(template).filter('#mainDiv');
        console.log(mainDiv);
        var errDiv = $(template).filter('#errDiv');
        console.log(errDiv);
        obj.find('tbody').append(mainDiv);

        obj.find('tbody').before(errDiv);
    }, 'html');
})
jQuery(函数($){
//模拟ajax请求
var html='1〕\
\\\
\\\
误差测试';
var obj=$(“表”)
$.post('/echo/html/'{
html:html
},函数(模板){
控制台日志(模板);
var mainDiv=$(模板).filter('#mainDiv');
控制台日志(mainDiv);
var errDiv=$(模板).filter('#errDiv');
console.log(errDiv);
obj.find('tbody').append(mainDiv);
对象find('tbody')。在(errDiv)之前;
}","html";;
})

最终对我有效的解决方案是在tr html周围包装一个表标记,然后从div元素中提取tr html。

下面是一个简化的场景示例。