開發時常遇到這種情境:主頁面有一張明細表,點某個操作的時候要彈出視窗,視窗裡的表格必須延續主頁面的資料,只是欄位需要調整、部分欄位要換成輸入框。做法有兩條路——重新查一次資料庫,或者直接把 DOM 值複製過去再改。本文示範後者。
<div class="materialCode stdtable">
<h2>發貨情況</h2>
<ul class="theadUl clearfix">
<li class="td1">發貨批次</li>
<li class="td2">商品</li>
<li class="td3">發貨數量</li>
<li class="td4">剩餘數量</li>
<li class="td5">收貨數量</li>
<li class="td6">收貨狀態</li>
<li class="td7">操作</li>
</ul>
<table id="table1">
<thead>
<tr>
<th class="td1"></th><th class="td2"></th><th class="td3"></th>
<th class="td4"></th><th class="td5"></th><th class="td6"></th>
<th class="td7"></th>
</tr>
</thead>
<tbody>
<c:forEach var="deliverGoodsList" items="${orderDetail.deliverGoodsList}" varStatus="outer">
<c:forEach var="deliverGoodsDetaiList" items="${deliverGoodsList.deliverGoodsDetaiList}" varStatus="inner">
<tr flag="${deliverGoodsList.deliverGoodsId}"
orderId="${deliverGoodsList.orderId}"
goodsId="${deliverGoodsDetaiList.goodsId}">
<c:if test="${inner.index == 0}">
<td rowspan="${deliverGoodsList.deliverGoodsDetaiList.size()}">${outer.index+1}</td>
</c:if>
<td>
<div class="ord_product">
<img src="${deliverGoodsDetaiList.goodsImage}">
<div>
<c:if test="${orderDetail.orderForm.orderType == '2'}">
<h5><a href="javascript:void(0);">${deliverGoodsDetaiList.goodsName}</a></h5>
</c:if>
<c:if test="${orderDetail.orderForm.orderType != '2'}">
<h5><a href="${URL_WWW}/shop/index.php?act=goods&goods_id=${deliverGoodsDetaiList.goodsId}">${deliverGoodsDetaiList.goodsName}</a></h5>
</c:if>
</div>
</div>
</td>
<td>${deliverGoodsDetaiList.deliveryNum}</td>
<td>${deliverGoodsDetaiList.overplusNum}</td>
<td>${deliverGoodsDetaiList.takeDeliveryNum}</td>
<c:if test="${inner.index == 0}">
<td rowspan="${deliverGoodsList.deliverGoodsDetaiList.size()}">
<c:if test="${deliverGoodsList.deliveryStatus == '10'}">待發貨</c:if>
<c:if test="${deliverGoodsList.deliveryStatus == '20'}">待確認收貨</c:if>
<c:if test="${deliverGoodsList.deliveryStatus == '30'}">已收貨</c:if>
<c:if test="${deliverGoodsList.deliveryStatus == '40'}">退貨</c:if>
</td>
</c:if>
<c:if test="${inner.index == 0}">
<td rowspan="${deliverGoodsList.deliverGoodsDetaiList.size()}">
<a href="javascript:;" class="pr beizhu">查看備註
<div class="beizhu_msg beizhu_msg_li" style="display: none;">
<img src="${URL_LIB}/static/order/images/sanjiao_orange.png">
<p>${deliverGoodsList.logisticsRemark}</p>
</div>
</a>
<c:if test="${deliverGoodsList.deliveryStatus != '30'}">
<input type="button" value="確認收貨" class="dayin"
onclick="showDeliver(${deliverGoodsList.deliverGoodsId})">
</c:if>
</td>
</c:if>
</tr>
</c:forEach>
</c:forEach>
</tbody>
<c:if test="${empty orderDetail.deliverGoodsList}">
<tbody>
<tr class="opt_zanwu"><td colspan="7">暫無發貨信息</td></tr>
</tbody>
</c:if>
</table>
</div>
<div id="messageBoxWin" class="popup"
style="display:none; z-index:1000; width:900px; height:500px;
left:50%; position:fixed; top:30%;
margin-left:-450px; margin-top:-125px; padding-bottom:20px;">
<div class="title">
<h2>確認收貨數量</h2>
<div>
<a class="min" href="javascript:;" title="最小化" style="display:none;"></a>
<a class="max" href="javascript:;" title="最大化" style="display:none;"></a>
<a class="revert" href="javascript:;" title="還原" style="display:none;"></a>
<a class="close" href="javascript:;" title="關閉"></a>
</div>
</div>
<div class="content" style="height:96%;">
<div style="height:70%; overflow:auto;">
<table class="recieveCount">
<thead>
<tr>
<th class="td1">發貨批次</th>
<th class="td2">商品</th>
<th class="td3">發貨數量</th>
<th class="td4">剩餘</th>
<th class="td5">收貨數量</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="8" style="color:#FF9400;">請仔細核對收貨數量後確認</td>
</tr>
</tbody>
</table>
</div>
<div class="bot_btns" style="position:absolute; bottom:30px; left:0; width:100%;">
<div class="ordSure_btn" style="width:100px; margin-left:30%; float:left;"
onclick="comformDeliver()">確定</div>
<div class="ordCancel_btn" style="width:100px; margin-left:10%; float:left;">取消</div>
</div>
</div>
</div>
function showDeliver(deliverId) {
// 挑出該批次的所有列,整段複製
var delitems = $("#table1 tr[flag='" + deliverId + "']").clone();
$(delitems).each(function (index) {
if (index == 0) {
// 第一行:批次欄位是 rowspan 合併而來,要移除狀態與操作欄
$($(this).find("td")[6]).remove();
$($(this).find("td")[5]).remove();
var deliverNum = $($(this).find("td")[2]).text();
var inputHTML = "<input type='text' onblur='checkAcount(this);'
class='sum' value='" + deliverNum + "'>";
$($(this).find("td")[4]).empty();
$($(this).find("td")[4]).append(inputHTML);
} else {
// 後續列沒有 rowspan 欄位,索引整體左移一
var deliverNum = $($(this).find("td")[1]).text();
var inputHTML = "<input type='text' onblur='checkAcount(this);'
class='sum' value='" + deliverNum + "'>";
$($(this).find("td")[3]).empty();
$($(this).find("td")[3]).append(inputHTML);
}
});
$("#messageBoxWin table tbody").empty();
$("#messageBoxWin table tbody").append(delitems);
deliverGoodsId = deliverId;
}
程式能跑,但它把「rowspan 造成的欄位錯位」用 index == 0 硬編碼處理掉了——第一行取 td[4]、其餘取 td[3]。這意味著只要主表格的欄位順序、rowspan 結構或欄位數量有任何變動,這些魔術數字就會全部失效,而且錯得很安靜。
更穩健的替代方案有兩個。一是用 class 而非位置選取:$(this).find('.td-takeNum'),讓結構變化不致於波及邏輯。二是根本不要複製 DOM,改以資料驅動——把主表格的資料存在一個 JS 物件裡,彈窗渲染時重新用資料產生表格。後者程式碼看起來多一點,但可讀性與可維護性都好得多,也不容易出現「複製來的 id 重複」這類隱藏問題(原範例複製後的 input 都沒有 id,算是剛好避開)。
另外提醒:用字串拼接 HTML 塞入使用者資料(如商品名稱)時,務必先轉義,否則資料中若含 <、" 等字元,可能破壞結構甚至形成 XSS 風險。改用 $('<input>').val(value) 這種以屬性設定的方式會安全許多。
以上就是這次分享的做法與檢討,希望對你的開發有所幫助。