【问题标题】:jQuery .load on tablerow is messing up table; adding an extra trtablerow 上的 jQuery .load 搞砸了表格;添加一个额外的 tr
【发布时间】:2016-01-23 00:42:01
【问题描述】:

我正在使用 jQuery 在输入更改时刷新表中的一行。这是我行的基本 html:

<tr id="TestCaseNameDataRow">
    <td><input value="some value that will be refreshed"></td>
    <td><select></select></td> // select that gets populated based on the above input 
</tr>

这是我刷新表格行的javascript:

$j('#TestCaseNameDataRow').load(window.location.href.split('?')[0] + ' #TestCaseNameDataRow', function(){
    // callback function       
});

但是,加载完成后,我的 html 看起来像这样:

<tr id="TestCaseNameDataRow">
    <tr id="TestCaseNameDataRow">
        <td><input value="some value that will be refreshed"></td>
        <td><select></select></td> // select that gets populated based on the above input 
    </tr>
</tr>

为什么.load() 将表格行加倍?我该如何阻止它?它让我的桌子看起来很不好看。

【问题讨论】:

    标签: javascript jquery html load html-table


    【解决方案1】:

    我的猜测是您正在从 load 调用中返回整行。试试这个看看:

    $j('#TestCaseNameDataRow').load(window.location.href.split('?')[0] + ' #TestCaseNameDataRow', function(response, status, xhr){ 
        console.log(response.data);
    });
    

    如果您在控制台中看到整个 tr,那么您将想要更改服务器返回的内容,或者将您的 .load 定位在父表上。

    【讨论】:

    • 我知道我要返回整行,但我不明白为什么要把它放在原来的 tr 中,而不是替换原来的 tr
    • 这就是 .load() 的作用。它填充它被调用的元素。如果你这样做 $j('#tableID').load(window.location.href.split('?')[0] + '#TestCaseNameDataRow');它应该可以满足您的需求。
    • 你也可以这样做: $j("#TestCaseNameDataRow").replaceWith(data);通过 $.get 或 $http $j.get("window.location.href.split('?')[0] + ' #TestCaseNameDataRow'", function(data) { $j( "#TestCaseNameDataRow").replaceWith(data); });
    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 2013-08-27
    相关资源
    最近更新 更多