//根据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;
}
}