开发者

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-childhelp 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜