繁体   English   中英

如何使用jQuery更新表格单元格值

[英]How to update table cell value using jQuery

我在使用jQuery 1.4.2更新表格单元格值时遇到问题。 这一切都适用于Firefox和Safari,但IE8和IE9根本没有做任何事情。 没有警告,错误或任何会给我一些提示在哪里寻找它的东西。

表如下:

<table id="test">
    <tr id="1">
        <td id="name">sample name</td>
        <td id="schedule">sample value</td>
        <td id="day">sample value</td>
    </tr>
    <tr id="2">
        <td id="name">sample name</td>
        <td id="schedule">sample value</td>
        <td id="day">sample value</td>
    </tr>
    <tr id="3">
        <td id="name">sample name</td>
        <td id="schedule">sample value</td>
        <td id="day">sample value</td>
    </tr>
</table>

我正在执行ajax调用并获取json数据:

{"Test": [
         {"id":"1", "name":"John", "day":"Monday"}, 
         {"id":"2", "name":"Marry", "day":"Thursday"} 
]}

一旦收到数据,就会有一个循环,它遍历json数据集并使用接收的数据更新相应的列,如下所示:

$.each(json.Tests, function(){
    /* update test with details */

    var test = this.hash;

    /*set values for each test */
    $("table#test tr[id=" + test + "]").find("#name").html(this.name);
    $("table#test tr[id=" + test + "]").find("#schedule").html(this.status);
    $("table#test tr[id=" + test + "]").find("#day").html(this.changed);
});

正如我所提到的,这已经在Safari和Firefox中测试过,一切正常,但IE8和IE9似乎没有做任何事情。

我认为我认为id属性应该保留给唯一标识符。 如何将td元素的id属性更改为类属性甚至名称属性。 我怀疑IE很混乱。

此外,如果您保持id唯一并将td的id属性更改为类,则可以将代码更改为:

$("#" + test + " td.name").html(this.name);

并且因为一个数字可以代表几乎任何东西,也可以为这些ID添加某种标识符前缀。 就像是:

$("#thing-" + test + " td.name").html(this.name);

而html看起来像这样:

<table id="test">
    <tr id="thing-1">
        <td class="name">sample name</td>
        <td class="schedule">sample value</td>
        <td class="day">sample value</td>
    </tr>
    <tr id="thing-2">
        <td class="name">sample name</td>
        <td class="schedule">sample value</td>
        <td class="day">sample value</td>
    </tr>
    <tr id="thing-3">
        <td class="name">sample name</td>
        <td class="schedule">sample value</td>
        <td class="day">sample value</td>
    </tr>
</table>

希望有所帮助!

Ids不应该以数字开头。 也许IE9不像其他浏览器那样宽容。

你有一个我们测试你的脚本的URL吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM