[英]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.