2011-03-30 84 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