对象 对象显示在 td 中

object Object is showing in td

本文关键字:对象 td 显示      更新时间:2023-09-26

当我尝试在tr下方添加多选时,它显示[对象对象]。

var counter = 0;
$("#addjs").click(function () {
    var anotherselect = $('#s1').clone();
    anotherselect.attr('id', "s1" + counter);
    $('tr').last().after("<tr><td></td> <td>" + anotherselect + "</td> <td> third row</td> <td>test</td> <td></td> </tr>");
    $("#s1" + counter).dropdownchecklist();
    counter++;
});

它正在工作,如果我确实喜欢下面

var counter = 0;
$("#addjs").click(function () {
    var anotherselect = $('#s1').clone();
    anotherselect.attr('id', "s1" + counter);
    $('tr').last().after(anotherselect);
    $("#s1" + counter).dropdownchecklist();
    counter++;
});

我需要附加 tr 和 td 而没有 [对象对象] 错误

很难确定,不知道anotherselect代表什么元素,但你应该能够使用 .get()outerHTML 来处理你真正连接一个字符串的事实:

var counter = 0;
$("#addjs").click(function () {
    var anotherselect = $('#s1').clone();
    anotherselect.attr('id', "s1" + counter);
    $('tr').last().after("<tr><td></td> <td>" + 
    // change just this part
    anotherselect.get(0).outerHTML + 
    "</td> <td> third row</td> <td>test</td> <td></td> </tr>");
    $("#s1" + counter).dropdownchecklist();
    counter++;
});

jQuery .get() 方法将为您提供anotherselect表示的实际 DOM 元素,outerHTML将对其进行序列化,以便您可以将其包含在标记字符串中。