您好,登錄后才能下訂單哦!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>layui動態(tài)添加刪除表格,并獲取表格中的值</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="css/layui.css" media="all">
<!-- 注意:如果你直接復制所有代碼到本地,上述css路徑需要改成你本地的 -->
<style>
</style>
</head>
<body style="padding: 10px">
<form class="layui-form layui-form-pane" action="">
<div class="layui-form-item">
<label class="layui-form-label">標題</label>
<div class="layui-input-block">
<input type="text" id="title" lay-verify="required" name="title" autocomplete="off" placeholder="請輸入標題" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">選項</label>
<div class="layui-input-inline">
<select id="gift" lay-verify="required" lay-search="">
<option value="">直接選擇或搜索選擇</option>
{foreach from=$info item=vo}
<option value="{$vo.id}">{$vo.title}</option>
{/foreach}
</select>
</div>
<label class="layui-form-label">輸入框</label>
<div class="layui-input-inline">
<input type="text" id="num" lay-verify="required" placeholder="請輸入" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-upload">
<button type="button" class="layui-btn layui-btn-normal" onclick="add()" id="testList">添加到表格</button>
<div class="layui-upload-list">
<table class="layui-table" id="tab">
<thead id="tb">
<tr><th>名稱1</th>
<th>名稱2</th>
<th>名稱3</th>
<th>操作</th>
</tr></thead>
<tbody id="demoList"></tbody>
</table>
</div>
</div>
<div class="layui-form-item">
<button class="layui-btn" lay-submit="" lay-filter="demo2">提交</button>
</div>
</form>
<script src="layui.all.js" charset="utf-8"></script>
<!-- 注意:如果你直接復制所有代碼到本地,上述js路徑需要改成你本地的 -->
<script>
var form = layui.form,layer = layui.layer;
var $ = layui.$;
var element = layui.element;
function add() {
var trObj = document.createElement("tr");
trObj.id = new Date().getTime();
trObj.innerHTML = "<th><input type='hidden' name='"+id+"[id]' value='"+id+"'>"+id+"</th><th>"+title+"</th><th><input type='hidden' name='"+id+"[num]' value='"+num+"'>"+num+"</th><th> <button onclick='del(this)' class='layui-btn layui-btn-xs layui-btn-danger'>刪除</button></th>";
document.getElementById("tb").appendChild(trObj);
}
function del(obj) {
var trId = obj.parentNode.parentNode.id;
var trObj = document.getElementById(trId);
document.getElementById("tb").removeChild(trObj);
}
//監(jiān)聽提交
form.on('submit(demo2)', function(data){
// layer.alert(JSON.stringify(data.field), {
// title: '最終的提交信息'
// })
var title=$("#title").val();
if(title==''){
layer.msg("標題不能為空!");
return false;
}
$.post("test.php",data.field,function(backdata){
layer.alert(backdata);
});
return false;
});
</script>
</body>
</html>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。