Jquery 如何在将复选框添加到表之前将其动态添加到每行

Jquery 如何在将复选框添加到表之前将其动态添加到每行,jquery,Jquery,我正在将Jquery datatable的行添加到表中 我的要求是在每行之前添加一个复选框 我已经试过了 $(this).append('<input type="checkbox"/>'); $("#greaterquan").append(this.outerHTML); $(this.append(“”); $(“#greaterquan”).append(this.outerHTML); 但这是行不通的 这是我的密码

我正在将Jquery datatable的行添加到表中

我的要求是在每行之前添加一个复选框

我已经试过了

$(this).append('<input type="checkbox"/>');            
            $("#greaterquan").append(this.outerHTML);
$(this.append(“”);
$(“#greaterquan”).append(this.outerHTML);
但这是行不通的

这是我的密码

    <table id="allwl">
        <th class="hidden-480">Price</th>
        <th class="hidden-480">Volume</th>
        <th class="hidden-480">Quantity</th>
    </table>




    <div id="greaterquan">
        <h3>My Stocks</h3>
    </div>



var dataSet = [
    [
        "1441.75",
        "34444444"],
    [
        "1614.45",
        "34444444"

    ],
    [
        "834.15",
        "233333"]

];

var array_names = ["APPLE", "WHIRLPOOL", "SAMSUNG"];

for (var key in dataSet) {
    if (dataSet.hasOwnProperty(key)) {
        dataSet[key].splice(0, 0, array_names[key]);
    }
}

$(function () {
    $('#allwl').dataTable({
        "iDisplayLength": -1,
            "data": dataSet,
            "columns": [{
            "title": "Name"
        }, {
            "title": "Price"
        }, {
            "title": "Quantity"
        }]
    });
});
$(document).ready(function(){
$('#allwl tr').each(function() {
    var abc = $(this).children('td').eq(2).html();
        if(abc > 40000) {
            $(this).children('td').eq(0).css('background-color', 'green');
//$(this).append('<input type="checkbox"/>');            
            $("#greaterquan").append(this.outerHTML);
        }

});
});

价格
卷
量
我的股票
变量数据集=[
[
"1441.75",
"34444444"],
[
"1614.45",
"34444444"
],
[
"834.15",
"233333"]
];
var array_name=[“苹果”、“惠而浦”、“三星”];
for(数据集中的var键){
if(dataSet.hasOwnProperty(键)){
数据集[key]。拼接(0,0,数组_名称[key]);
}
}
$(函数(){
$('#allwl')。数据表({
“iDisplayLength”:-1,
“数据”:数据集,
“栏目”:[{
“标题”:“名称”
}, {
“标题”:“价格”
}, {
“标题”:“数量”
}]
});
});
$(文档).ready(函数(){
$('#allwl tr')。每个(函数(){
var abc=$(this.children('td').eq(2.html();
如果(abc>40000){
$(this).children('td').eq(0).css('background-color','green');
//$(此)。附加(“”);
$(“#greaterquan”).append(this.outerHTML);
}
});
});

如果我理解正确,您可以尝试以下方法:

$("<input />", {"type": "checkbox"}).insertBefore($(this));
$(“”,{“type”:“checkbox”}).insertBefore($(this));
如果要在之前添加它,而不是附加/前置

但是,如果要追加/前置,应尝试以下操作:

附加:

($(this)).append($("<input />", {"type": "checkbox"})); 
($(this)).append($(“”,{“type”:“checkbox”});
预编:

($(this)).prepend($("<input />", {"type": "checkbox"}));
($(this)).prepend($(“”,{“type”:“checkbox”});
试试这个

//跳过
如果(abc>40000){
$(this).children('td').eq(0).css('background-color','green');
$(“#greaterquan”)。追加(
$(this).clone()//获取TR的克隆,因为源元素已附加到DOM
.children('td').first()//获取第一个td
.prepend(“”)//在TD内容之前添加复选框
.parent()//返回TR并将其附加到“greaterthan”元素
);
}

试试这个,非常感谢,是否可以只在greaterquan div下添加复选框
    // skipped
    if(abc > 40000) {
        $(this).children('td').eq(0).css('background-color', 'green');
        $("#greaterquan").append(
            $(this).clone() // get clone of TR because source element is attached to DOM
                .children('td').first() // get first TD
                .prepend('<input type="checkbox"/>') // add checkbox before content of TD
                .parent() // return to TR and append it to "greaterthan" element
        );
    }