我想动态创建新表并使用jquery通过相同的按钮删除它?

时间:2015-12-07 06:19:59

标签: javascript jquery

我想每次都显示更新的行。我有这个代码。想要删除并再次生成同样的

var table = $('#table').children();
for (var j = i; j < result.length; j++) {
    table.append("<tr><td>" + (j + 1) + "</td><td><b>" + result[j].VoterName + "</b></td><td><b>" + result[j].VoterContact + "</b></td><td><button>X</button></td></tr>");
    i++;
}

1 个答案:

答案 0 :(得分:0)

function createOrDeleteTable(){
  console.log("wadawdawd");
  if($("#btn").attr("created") === "false") {
    $("#btn").attr("created","true");
    
    var tabledata = "<table>"
                + "<tr>" 
                  +"<th> hey </th>"
                  +"<th> hello </th>"
                + "</tr>"
              + "</table>";
    console.log(tabledata);
    $("#appendHere").html(tabledata);
  } else {
    $("#btn").attr("created","false");
    $("#appendHere").html("");
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="createOrDeleteTable();" created="false" id="btn"> create table </button>

<div id="appendHere"> </div>