Jquery 動態爲table添加行

//新增試卷_添加行 
function addtr(id) {
	tr_id = $("#tQuestion>tbody>tr:last").attr("id");
	tr_id++;
	$('#' + id).append(GetStr(tr_id));
}
//新增試卷_刪除行 
function deltr(id) {
	tr_id = $("#tQuestion>tbody>tr:last").attr("id");
	if (tr_id != 1) {
		$('#' + tr_id).remove();
	}
}
//動態添加一行 
function GetStr(tr_id) {
	var str = "<tr id='" + tr_id + "'><td><input type=\"text\" id=\"TxtTitle_"
			+ tr_id
			+ "\" name="
			+ tr_id
			+ " value=\"\"  style=\"width:90%\"/></td>";
	str += "<td><textarea style=\"width:90%; border: 1px solid rgb(164, 194, 204);\" id=\"TxtPrompt_"
			+ tr_id
			+ "\" cols=\"20\" rows=\"2\" name="
			+ tr_id
			+ "></textarea></td>";
	str += "<td><input type=\"button\" class=\"normalBtn\" id=\"btnQuestionAdd\" value=\"新增\" name=\"btnQuestionAdd\" onclick=\"addtr('tQuestion')\">";
	str += " <input type=\"button\" class=\"normalBtn\" id=\"btnQuestionDel\" value=\"刪除\" name=\"btnQuestionDel\" onclick=\"deltr('tQuestion')\"></td>";
	str += "</tr>";
	return str;
}

$(function() {
	$('#tQuestion').append(GetStr(1));
})


<div>
<table cellspacing="0" id="tQuestion" border="1" rules="all" class="mainTab" style="width: 100%"> 
 <tr> 
    <th> 
        大題名稱 
 </th> 
     <th> 
         大題說明 
  </th> 
      <th> 
         操做 
  </th> 
 </tr>

</table> app

</div>
相關文章
相關標籤/搜索