2011-03-30 83 views
4

我遇到了使用jQuery 1.4.2更新表格单元格值的问题。它全部在Firefox和Safari中运行,但IE8和IE9根本没有做任何事情。没有警告,错误或任何会给我一些提示寻找它的提示。如何使用jQuery更新表格单元格值

表看起来如下:

<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); 
}); 

正如我所提到的,这是en在Safari和Firefox中测试都很好,但IE8和IE9似乎没有做任何事情。

回答

6

我认为id属性应该保留为唯一标识符在我看来。如何将td元素的id属性更改为类属性或甚至名称属性。我怀疑IE越来越困惑。

另外,如果你把IDS独特而改变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> 

希望帮助!

+1

这是一个JSFiddle,显示了这一点(以及一些JavaScript修复)http://jsfiddle.net/eFHZz/4/ – 2011-08-05 23:53:49

+0

感谢您的回复,这种方式很有效! – m1k3y3 2011-11-02 12:42:23

0

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

+0

好吧,让我测试它。 – m1k3y3 2011-03-30 10:23:39

+0

nope,它没有帮助 – m1k3y3 2011-03-30 10:27:33

0

您有我们的网址来测试您的脚本吗?

+0

不幸的是它只在本地主机上 – m1k3y3 2011-03-30 10:44:20

+0

你能上传一个Demoscript吗? – 2011-03-30 10:51:16