jquery遍历数组添加行删除行

时间:2022-02-02 09:39:41

<script language="javascript" type="text/javascript">

function queryInfo() {
var membercode = $('#membercode').val();
if(''== $.trim(membercode)){
alert('请填写会员编号!');
return false;
}
removeTr();
var formUrl = '${rc.contextPath}/debitLimitMgt/queryList.htm?method=queryList';
$.ajax({
url : formUrl,
type : 'POST',
dataType : 'json',
data : {'membercode': membercode},
async : false,
success : function(returnMsg) {
addTr(returnMsg);
},
error: function(jqXHR, textStatus, errorMsg){
alert("请求失败:" + errorMsg);
}
});
}

function removeTr(){
var rows=$("#dataList").find("tr").length;
if(rows>1){
for(var i=1;i<rows;i++){
$("#dataList tr").eq(i).remove();
}
}
}

function addTr(data){
$.each(data,function(i,obj){
var trHtml = '<tr><td>'+obj.membercode+'</td><td>'+obj.memberName+'</td><td>'+
obj.idContent+'</td><td>'+obj.memberType+'</td><td>'+
obj.openDebit+'</td><td>'+obj.singleLimit+'</td><td>'+
obj.batchLimit+'</td><td>'+obj.filterNum+'</td><td><tr>';
$("#dataList tr:last").after(trHtml);
})
}
</script>