首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按钮在动态表中显示为[ Object ]

按钮在动态表中显示为[ Object ]
EN

Stack Overflow用户
提问于 2015-11-22 18:08:56
回答 1查看 213关注 0票数 0

我试图使用jquery创建一个待办事项列表,并希望向我的表中添加一个delete按钮。当delete按钮显示在表中时,它只表示Object。有人能帮忙吗?下面是我的jQuery和HTML。我的jQuery的最后一行是添加删除按钮的地方。

代码语言:javascript
复制
$(document).ready(function() {
  //Add the importance level to the first column of the to do table
  $("#submit-button").on("click", function addTask() {
    var taskToDo = $("#comment").val();
    var newRow = $("<tr>");
    var newTd = $("<td>");
    var deleteBtn = $("<button>").addClass("btn btn-danger").append("X");

    //Append the importance ranking to the table
    if($('#vimp').is(':checked'))
      alert("Your task is very important")
      newRow.append("<td>X</td>");
      /*var bullet = $("&bull;").css("color","red");
      $("#to-do-list").append(newRow);
        newRow.append('<td>'+bullet+'</td>');*/

    if($('#simp').is(':checked'))
      alert("Your task is somewhat important")

    if($('#canwait').is(':checked'))
      alert("Your task can wait")
  
    //Append the task to the table
    $("#to-do-list").append(newRow);
      //newRow.append("<td>X</td>");
      newRow.append('<td>'+taskToDo+'</td>');
      newRow.append('<td>'+deleteBtn+'</td>');

  });

});
代码语言:javascript
复制
      <table class="table table-striped table-responsive table-hover" id="col-width">
        <col width="30px">
        <col width="400px">
        <thead>
          <tr>
            <td>Importance</td>
            <td>Task</td>
            <td>Delete button</td>
          </tr>
        </thead>
        <tbody id="to-do-list">
        </tbody>
      </table>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-22 18:29:32

deleteBtn是一个jQuery对象,但是'<td>'+deleteBtn+'</td>'中的+操作符需要一个字符串,因此该对象自动转换为它的字符串表示形式。

要解决这个问题,您需要首先附加td,然后将按钮附加到td中,例如:

代码语言:javascript
复制
newRow.append('<td>').find('td').last().append(deleteBtn);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33858256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档