简体   繁体   中英

How to insert a checkbox before an element by jQuery

I have generated a table with JQuery csv2table plugin. How could I insert checkboxes in every table box?

I don't know the plugin, but in general .append() help and .appendTo() help should help you out here.

$('<input>', {
    type:  'checkbox'
}).appendTo(/* "#tableid" or "#tableid td" or "#tableid .tablebox" */ );

Update
In referrence to your comment. You can use jQuerys :nth-child help selector to have a query like I believe you want. For instance

.appendTo($('#table').find('tr:nth-child(3n)').find('td:nth-child(8n)'));

Or you can also use the insertBefore method:

Example:

$('<input type="checkbox" name="namehere" />').insertBefore('selector');

Assuming you want a unique id for each checkbox you can .append() the input using the .each() method in order to use the index for the id :

$('#yourTable').find('td').each(function(i,obj){
  $(obj).append('<input type="checkbox" id="checkbox'+i+'" name="checkbox'+i+'">');
});
$(function(){
    var counter=1;
    var chk="<input type='checkbox' id='chk_'"+counter+"'/>";

    $("table#t tr td").each(function(){
        $(this).append(chk);
        counter++;
    });
});

try here

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