页面添加文本框
当点击加号时,在当前行的下一行会增加相同的一行;当点击减号时,会自动给移出。
HTML页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<center>
<form action="{:url('index/add_do')}">
<table>
<tr>
商品名称 :{$data[0]['g_name']}
货号 : {$data[0]['hp_num']}
<input type="hidden" name="goods_id" value="{$data[0]['g_id']}">
</tr>
<tr>
<td>颜色</td>
<td>内存</td>
<td>货号</td>
<td>库存</td>
<td>操作</td>
</tr>
{volist name="data" id="v"}
<tr>
<td>
<select name="colors[]" id="">
{volist name="colors" id="vv"}
<option value="{$vv.a_id}" <?php if($v['attr'][0]==$vv['a_val']){echo "selected";} ?>>
{$vv['a_val']}
</option>
{/volist}
</select>
</td>
<td>
<select name="buffers[]" id="">
{volist name="buffers" id="vv"}
<option value="{$vv['a_id']}" <?php if($v['attr'][1]==$vv['a_val']) echo "selected"; ?>>
{$vv['a_val']}
</option>
{/volist}
</select>
</td>
<td><input type="text" name="hp_num[]" value="{$v['hp_num']}"></td>
<td><input type="text" name="stock[]" value="{$v['stock']}"></td>
<td><input type="button" value="+" class="jia"></td>
</tr>
{/volist}
<tr>
<td colspan="5" align="center"><input type="submit" value="保存"></td>
</tr>
</table>
</form>
</center>
<script src="__STATIC__/jquery.min.js"></script>
<script>
//加
$(document).on("click",".jia",function () {
var obj=$(this).parents("tr").clone();
obj.find(".jia").attr("value","-");
obj.find(".jia").attr("class","jian");
$(this).parents("tr").after(obj);
})
//减
$(document).on("click",".jian",function () {
$(this).parents("tr").remove();
})
</script>
</body>
</html>
PHP页面
<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
class Index extends Controller
{
public function index()
{
$goods_id=1;
$data=Db::table("week3_goods")
->join("week3_hp","week3_goods.g_id=week3_hp.goods_id")
->join("week3_goods_attr","week3_hp.g_a_id=week3_goods_attr.g_a_id")
->where("week3_goods.g_id=$goods_id")
->select();
//var_dump($data);
foreach ($data as $k=>$v)
{
if($v['a_id'])
{
$ids=explode(",",$v['a_id']);
foreach ($ids as $kk=>$vv)
{
$res=Db::table("week3_attr")->where("a_id",$vv)->find();
$ids[$kk]=$res['a_val'];
$data[$k]['attr']=$ids;
}
}
}
$colors=Db::table("week3_attr")->where("a_name","颜色")->select();
$buffers=Db::table("week3_attr")->where("a_name","内存")->select();
//var_dump($colors);
return view("show",['data'=>$data,'colors'=>$colors,'buffers'=>$buffers]);
}
//批量添加
public function add_do()
{
$data=Request::instance()->param();
// var_dump($data['goods_id']);die;
foreach ($data['colors'] as $k => $v){
$arr['goods_id'] = $data['goods_id'];
$arr['attr'] = $data['colors'][$k].','.$data['buffers'][$k];
$arr['hp_num'] = $data['hp_num'][$k];
$arr['stock'] = $data['stock'][$k];
$res[] = $arr;
}
print_r($res);
}
}
你所浪费的今天是那些死去的人所奢望的明天,你所厌恶的现在是未来的你所回不去的曾经。