如何使用javascript覆盖按钮单击时动态创建的表的行值

时间:2013-09-18 11:43:54

标签: javascript html xml xpath

我有一个用户可以选择“旧”,“新”等选项的下拉列表。根据所选的选项,我从xml查询数据并将其显示在表格中。

HTML

<body>
<table id="NovelList" class="NovelList" border="1">
<tr>
<th>Novel Title</th>
<th>Author</th>
<th>Publishing Year</th>
<th>Price</th>
<th>Purchase</th>
</tr>
</table>

</body>

并且为了在选项选择上向表添加数据我正在这样做: -

var nodes = xmlDoc.evaluate(pathExpr, xmlDoc, nsResolver, XPathResult.ANY_TYPE, null);
        var result = nodes.iterateNext();
        while (result) {
            var b= [];
            var d=result.childNodes;
            for(var i=0;i<d.length;i++){
                if(d[i].nodeType == 1){
                    b.push(d[i]);
                    }
                }
            for(var p=0;p<b.length;p++){
                    var n = b[p];
                    if (n.localName== "title"){var t = n.childNodes[0].nodeValue;}
                            if (n.localName== "author"){var a = n.childNodes[0].nodeValue;}
                            if (n.localName== "year"){var y = n.childNodes[0].nodeValue;}
                            if (n.localName== "price"){var pr = n.childNodes[0].nodeValue;}
                            if (n.localName== "code"){var c = n.childNodes[0].nodeValue;}

                    }
            $('.NovelList tr:last').after(
                    '<tr><td class="value">'+ t
                            + '</td><td class="value">'+ a
                            + '</td><td class="value">' + y
                            + '</td><td class="value">' + pr
                            + '</td><td class="chk"><input type="checkbox" name="chkbox" value='+'"'+c+'"'+'></td></tr>');
        result = nodes.iterateNext();
        }
    }

但是当我使用“.after”时,每次我选择一个选项时,它都会附加到已经显示的表格中。

我想在选择选项时替换表格行。

2 个答案:

答案 0 :(得分:0)

首先,将头部和身体分开:

<table>
    <thead>
        <tr>...</tr>
    </thead>
    <tbody>
        <tr>...</tr>
    </tbody>
</table>

然后致电

$('tbody').empty();

应该做的伎俩。基本上,您需要先擦除DOM。

答案 1 :(得分:0)

感谢Fenixp的帮助。我在表中添加了header和body标签,但是后面也是空的()没有工作,所以我最终使用了remove()。

编辑后的HTML文件

<table id="NovelList" class="NovelList" border="1">
                    <thead>
                        <tr>
                            <th>Novel Title</th>
                            <th>Author</th>
                            <th>Publishing Year</th>
                            <th>Price</th>
                            <th>Purchase</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr></tr>
                    </tbody>
                </table>

在添加行之前调用它 -

$('#NovelList tbody >tr.dtr').remove();

并使用

添加行
$('.NovelList tbody tr:last').after(
                    '<tr class="dtr"><td class="value">'+ t[0].firstChild.nodeValue
                        + '</td><td class="value">'+ a[0].firstChild.nodeValue
                        + '</td><td class="value">' + y[0].firstChild.nodeValue
                        + '</td><td class="value">' + p[0].firstChild.nodeValue
                        + '</td><td class="chk"><input type="checkbox" name="chkbox" value='+'"'+code+'"'+'></td></tr>');