如何将表格单元格添加到表格行

JQuery How to add table cell to a table row

本文关键字:表格 添加 单元格      更新时间:2023-09-26

我正在尝试使用jQuery动态添加单元格到我的表行。

HTML:

<table class="col-md-12" id="location-table">
    <tr id="location-row">
        <td>
            <label>
                <input type="radio" id="location-radio"  value="other" />
                Other location
            </label>
        </td>
    </tr>
</table>

JS/JQuery(这两种方法我都试过了):

$('<td><label><input type="radio" id="location-radio" value="test" />Test</label></td>').appendTo('#location-row');
$('#location-row').append('<td><label><input type="radio" id="location-radio" value="test" />Test</label></td>');

非常感谢所有的帮助

你的代码应该可以工作了。

$('<td><label><input type="radio" id="location-radio" value="test" />Test</label></td>').appendTo('#location-row');
$('#location-row').append('<td><label><input type="radio" id="location-radio" value="test" />Test</label></td>');

这不是最优雅的解决方案,但都可以在jsfield中工作…

http://jsfiddle.net/d5AdX/

你是否正确加载jquery ?

如果你在网站上打开chrome开发控制台并提交一个美元符号($),它应该告诉你这是一个函数。如果在右边它没有一个链接到一个js文件的行号(像'jquery.min.js:2'),那么jquery没有加载在你的页面上,你需要确保你的头脚本是正确的。

这里有一个链接到谷歌CDN,在那里你可以找到所有的jquery脚本:https://developers.google.com/speed/libraries/devguide jquery