今天前端大佬幫我解決了一個棘手的問題:克隆了已有系統的網頁,嘗試把復制下來的html改造成jsp。基本功能正常,然而點擊新增按鈕,出來的行無法點擊下拉選,控制台也沒報錯。
項目用的是jeesite2.0框架,springMVC架構。下拉選默認使用了select2插件,
源代碼:
<script type="text/javascript">
//重新生成其他費用索引
function resetOtherFeeListIndex() {
$('#tblOtherFee').find("tr.otherFeeDataRow").each(function(i) {
var trJQ = $(this);
trJQ.find("[name$='othfeeId']").attr("id", "othfeeId" + i).attr("name", "mawb.awbOthfeeDtos[" + i + "].othfeeId");
trJQ.find("[name$='othfeeOwner']").attr("id", "othfeeOwner" + i).attr("name", "mawb.awbOthfeeDtos[" + i + "].othfeeOwner");
trJQ.find("[name$='othfeeRate']").attr("id", "othfeeRate" + i).attr("name", "mawb.awbOthfeeDtos[" + i + "].othfeeRate");
trJQ.find("[name$='othfee']").attr("id", "othfee" + i).attr("name", "mawb.awbOthfeeDtos[" + i + "].othfee");
});
}
//添加其他費用
function addOtherFeeRow(othfeeId, othfeeOwner, othfeeRate, othfee, othfeeDesc) {
var trJQ = $($('#tblOtherFeeTemplate tbody').html());
if (othfeeId) {
trJQ.find("[name$='othfeeId']").val(othfeeId);
}
$('#tblOtherFee').append(trJQ);
resetOtherFeeListIndex();
}
$(function() {
//添加其他費用
$('#otherFeeAddBtn').click(function() {
addOtherFeeRow();
return false;
});
//刪除其他費用
$('tr.otherFeeDataRow').find(".otherFeeDeleteBtn").live("click", function() {
$(this).parents("tr.otherFeeDataRow:first").remove();
resetOtherFeeListIndex();
return false;
});
});
</script>
1 <table id="tblOtherFeeTemplate" style="display: none;"> 2 <tbody> 3 <tr class="otherFeeDataRow"> 4 <td> </td> 5 <td align="center"> 6 7 8 9 <select name="othfeeId" style="width:120px;"> 10 <option value="">-請選擇-</option> 11 <option value="AW">AW/制單費</option> 12 <option value="CG">CG/港口費</option> 13 <option value="LA">LA/活動物操作費</option> 14 <option value="MA">MA/歸屬代理人雜費</option> 15 <option value="MO">MO/雜費</option> 16 <option value="MY">MY/燃油費</option> 17 <option value="RA">RA/危險品操作費</option> 18 <option value="SC">SC/安全附加費</option> 19 <option value="SD">SD/地面運費</option> 20 <option value="TX">TX/稅</option> 21 <option value="VC">VC/聲明價值附加費</option> 22 23 24 </select> 25 26 27 28 </td> 29 <td align="center"> 30 <input type="text" name="othfeeOwner" maxlength="1" value="C" readonly="readonly" tabindex="-1" class="inputText ime-off" style="width: 30px;background-color:#DDDDDD;text-align:center;"/> 31 </td> 32 <td align="center"> 33 <input type="text" name="othfeeRate" maxlength="8" value="" class="inputText ime-off" style="width: 50px;"/> 34 </td> 35 <td align="center"> 36 <input type="text" name="othfee" maxlength="8" value="" class="inputText ime-off" style="width: 80px;"/> 37 </td> 38 <td style="text-align:center;padding: 0px;vertical-align: middle;"> 39 <a href="javascript:void(0);" class="otherFeeDeleteBtn" tabindex="-1"> 40 <img src="/static/global/icons/delete16X16.png" border="0" style="margin-top:2px;"> 41 </a> 42 </td> 43 </tr> 44 </tbody> 45 </table> 46 47 48 49 50 <div style="width: 95%; height: 150px; overflow: auto; position: relative;"> 51 <table border="0" cellspacing="0" cellpadding="0" class="table-form Tlist" id="tblOtherFee" style="margin-left:2px;"> 52 <tr> 53 <td style="width:70px;" align="center"><label class="formbox-title">其他費用</label></td> 54 <td style="width:120px;" align="center"><label class="formbox-title">代碼</label></td> 55 <td style="width:50px;" align="center"><label class="formbox-title">歸屬人</label></td> 56 <td style="width:70px;" align="center"><label class="formbox-title">費率</label></td> 57 <td style="width:90px;" align="center"><label class="formbox-title">費用</label></td> 58 <td style="width:35px;text-align:center;padding: 0px;vertical-align: middle;"> 59 <a href="javascript:void(0);" id="otherFeeAddBtn" tabindex="-1"> 60 <img src="/static/global/icons/plus16X16.png" border="0" align="middle" style="margin-top:2px;"/> 61 </a> 62 </td> 63 </tr> 64 65 <script type="text/javascript"> 66 //頁面剛剛加載時默認調用的方法,生成一個數據行,其中的下拉選可以點擊 67 addOtherFeeRow("MY","C","0.2",""); 68 69 </script> 70 71 72 </table> 73 </div>
實際效果圖:
改進的代碼:
1 var selectTemp = $('<select name="othfeeId" style="width:120px;"><option value="">-請選擇-</option><option value="AW">AW/制單費</option><option value="CG">CG/港口費</option><option value="LA">LA/活動物操作費</option><option value="MA">MA/歸屬代理人雜費</option><option value="MO">MO/雜費</option> <option value="MY">MY/燃油費</option> <option value="RA">RA/危險品操作費</option> <option value="SC">SC/安全附加費</option> <option value="SD">SD/地面運費</option> <option value="TX">TX/稅</option> <option value="VC">VC/聲明價值附加費</option></select>'); 2 if (othfeeId) { 3 //trJQ.find("[name$='othfeeId']").val(othfeeId); 4 selectTemp.val(othfeeId); 5 } 6 //刪除了下拉選的html內容,並在td中加入class=‘othfeeSelect’,用來定位便於append進Dom元素 7 trJQ.find('.othfeeSelect').append(selectTemp); 8 $('#tblOtherFee').append(trJQ); 9 //渲染新加入的下拉選 10 $("select[name='othfeeId']:last").select2();