简体   繁体   English

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

[英]How to update table cell value using jQuery

I am having problem updating table cell value using jQuery 1.4.2. 我在使用jQuery 1.4.2更新表格单元格值时遇到问题。 it all works in Firefox and Safari but IE8 and IE9 is simply not doing anything. 这一切都适用于Firefox和Safari,但IE8和IE9根本没有做任何事情。 There is no warning, error or anything that would give me some hint where to look for it. 没有警告,错误或任何会给我一些提示在哪里寻找它的东西。

Table looks following: 表如下:

<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>

I am executing ajax call and getting json data: 我正在执行ajax调用并获取json数据:

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

once data is received there is a loop which iterates through the json dataset and updates appropriate column with received data as following: 一旦收到数据,就会有一个循环,它遍历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);
});

As I mentioned, this has been tested in Safari and Firefox all works fine but IE8 and IE9 seems not to do anything. 正如我所提到的,这已经在Safari和Firefox中测试过,一切正常,但IE8和IE9似乎没有做任何事情。

I think the id attribute should be reserved for unique identifiers in my opinion. 我认为我认为id属性应该保留给唯一标识符。 How about changing the id attribute of the td elements to a class attribute or even name attribute. 如何将td元素的id属性更改为类属性甚至名称属性。 I suspect that IE is getting confused. 我怀疑IE很混乱。

Also, if you keep ids unique and change the id attribute of the td to a class then you can change your code to something like: 此外,如果您保持id唯一并将td的id属性更改为类,则可以将代码更改为:

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

And because a number could represent pretty much anything also prefixing those ids with some sort of identifier prefix would be good. 并且因为一个数字可以代表几乎任何东西,也可以为这些ID添加某种标识符前缀。 Something like: 就像是:

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

And the html would look like this: 而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>

Hope that helps! 希望有所帮助!

Ids aren't supposed to start with a number. Ids不应该以数字开头。 Perhaps IE9 isn't as forgiving as the other browsers. 也许IE9不像其他浏览器那样宽容。

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

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

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