写一个js 操作table,结合php,添加删除行操作

16 篇文章 0 订阅
//根据table  ID添加相应的行
	function addrow(){
		
		var myTable = document.getElementById("tb1");
		var newRow = tb1.insertRow(tb1.rows.length);
		newRow.align="center";
		var newTd1 = newRow.insertCell(0);
		newTd1.innerHTML="单品"+(tb1.rows.length-1); 
		var newTd2 = newRow.insertCell(1);
		newTd2.innerHTML="<input type=\"text\"   value=\"\"     class=\"txt\"   width=\"150px\" name=\"prono[]\"  />";
		var newTd3 = newRow.insertCell(2);
		newTd3.innerHTML="<input name=\"proname[]\"  type=\"text\" value=\"\"   class=\"txt\"  />";
		var newTd4 = newRow.insertCell(3);
		newTd4.innerHTML="<input name=\"num[]\"  type=\"text\" value=\"1\"   class=\"txt\"    />";
		var newTd5 = newRow.insertCell(4);
		newTd5.innerHTML="<input name=\"price[]\" type=\"text\" value=\"0\"    class=\"txt\"  />";
		var newTd6 = newRow.insertCell(5);
		newTd6.innerHTML="<a href=\"javascript:void();\" οnclick='{if(confirm(\"确定要删除?\")) {deleteCurrentRow(this,0); }else {}}'>删除</a>";

	}
//删除选中行tr
	function deleteCurrentRow(obj,id){  
		if(id!=0){

			if(ajaxdel(id)){
			
				var tr=obj.parentNode.parentNode;    
				var tbody=tr.parentNode; 
				tbody.removeChild(tr);   
			
			}else{
			
			
				alert("网络问题,删除失败,请联检查网络!");
			
			}

		}else{
		
			var tr=obj.parentNode.parentNode;   
			 
			var tbody=tr.parentNode;
			 
			tbody.removeChild(tr);   
		}
		
		
	}

//异步删除
	function  ajaxdel(id){
	
		var bol=false;
		
		$.ajax({
				type:"POST",
				async:false,
				url:"pro_edit.php",
				data:"id="+id+"&dopost=ajaxdel",
				success:function(data){
					alert(data);
					if(data=='success'){
						bol=true;
					}
					
				},error:function(data){
						bol=false;
				}});
				
				
		return bol;
	
	}

部分html代码

<button name="button1" type="button" οnclick="addrow();">添加产品</button>
		<div class="list-div">
			<table id="tb1" >
				<tr > 
					<th  align="center" height="30"></th>
					<th  align="center" height="30">产品编号</th>
					<th  height="30">产品名称</th>
					<th  height="30">数量</th>
					<th  height="30">单价</th>
					 <th  height="30">操作</th> 
				</tr>
				<?php 
				
					$dsql->SetQuery($sql_list);
					$dsql->Execute();
					$i=0;
					while($r=$dsql->GetObject()){ $i++; ?> 
				<tr align="center" >
					<td >单品<?php  echo $i ; ?></td>
					<input type="hidden" name="ids[]" value="<?php echo $r->id ?>" />
					<td ><input name="prono[]"  type="text"   value="<?php echo $r->prono ?>"   οnblur="saveDetail(<?php echo $r->id  ?>,'prono',this.value)"  class="txt"   width="150px"  /></td>
					<td ><input name="proname[]" type="text" value="<?php echo $r->proname ?>"   οnblur="saveDetail(<?php echo $r->id  ?>,'proname',this.value)" class="txt"  /></td>
					<td ><input name="num[]" type="text" value="<?php echo $r->num ?>"  οnblur="saveDetail(<?php echo $r->id  ?>,'num',this.value)" class="txt"    /></td>
					<td ><input name="price[]" type="text" value="<?php echo $r->price ?>"  οnblur="saveDetail(<?php echo $r->id  ?>,'price',this.value)"  class="txt"  /></td>
					<td ><a href="javascript:void();" οnclick='{if(confirm("确定要删除?")) {deleteCurrentRow(this,"<?php echo $r->id ?>" ); }else {}}'>删除</a> </td>
				</tr>
				<?php } ?>				
			</table>
		</div>	  


php代码

	if($dopost=="ajaxdel"){
	
		if(empty($id))
		{
			echo "fail";exit;
		}
		
		$result=$dsql->ExecuteNoneQuery2("Delete From `#@__product` where id='$id' ");;
		if($result>0){
			
			echo "success";exit;
			
		}else{	
			echo "fail";exit;
		}	

			
	}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值