最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to update table cell value using jQuery - Stack Overflow

programmeradmin5浏览0评论

I am having problem updating table cell value using jQuery 1.4.2. it all works in Firefox and Safari but IE8 and IE9 is simply not doing anything. 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:

{"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:

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

I am having problem updating table cell value using jQuery 1.4.2. it all works in Firefox and Safari but IE8 and IE9 is simply not doing anything. 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:

{"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:

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

Share Improve this question edited May 15, 2017 at 7:55 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 30, 2011 at 10:18 m1k3y3m1k3y3 2,7988 gold badges40 silver badges68 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 11

I think the id attribute should be reserved for unique identifiers in my opinion. How about changing the id attribute of the td elements to a class attribute or even name attribute. I suspect that IE is getting confused.

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:

$("#" + 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. Something like:

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

And the html would look like this:

<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. Perhaps IE9 isn't as forgiving as the other browsers.

Have you an URL for us to Test your Script?

发布评论

评论列表(0)

  1. 暂无评论