简体   繁体   中英

JQuery How to add table cell to a table row

I'm trying to use jQuery to dynamically add cells to my table row.

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 (I've tried both of these methods):

$('<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>');

All help is very much appreciated.

Your code should work..

$('<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>');

It's not the most elegant solution, but both work in jsfiddle...

http://jsfiddle.net/d5AdX/

Are you loading jquery correctly?

if you open the chrome dev console while on your site and submit a dollar sign($) it should tell you it's a function. if on the right it doesn't have a link to a js file with a line number(something like 'jquery.min.js:2') then jquery isn't loading on your page and you need to make sure your head script is right.

here's a link to the google cdn where you can find all the jquery scripts: https://developers.google.com/speed/libraries/devguide#jquery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM