layer彈出框的content使用html內容時
比如:
下面是html內容
<div id="contentDiv">
<div style="width:350px;margin:10px auto;text-align:center;">
<form >
<div id="aa1">
名稱: <input type="text" id="userName" />
</div>
<div id="aa2" style="margin-top:20px;">
學號: <input type="text" id="number" />
</div>
<div id="aa3" style="margin-top:20px;">
班級: <input type="text" id="stuclass" />
</div>
</form>
</div>
</div>
在彈出框打開前先給文本框賦值,為了彈出框里的文本框也有值。代碼如下:
//給修改綁定事件
$(".myUpdate").click(function () {
var nowRows = $(this);//當前選中的行
var uid = nowRows.parent().siblings().eq(0).html();//id
$("#userName").val( nowRows.parent().siblings().eq(1).html());//用戶名
$("#number").val( nowRows.parent().siblings().eq(2).html());//學號
$("#stuclass").val(nowRows.parent().siblings().eq(3).html());//班級
然后再將彈出框里的正文改為div的id為contentDiv盒子里的所有html,相當於復制一個。
這樣再打開彈窗時會出現div的id為contentDiv盒子里的所有內容,但是文本框取沒有值,前一個div的id為contentDiv盒子里卻有值了,這是因為實際上存在兩個id為contentDiv的盒子,而選擇器賦值時默認找了第一個id為contentDiv的盒子,可以將彈出框里的那個盒子理解為未來生成的。
解決方法
直接將彈出框的正文改為改為div的id為contentDiv盒子本身,這樣就一直都只有一個div了,然后設置一下基本層類型即可。
這樣的話,div的id為contentDiv盒子會直接被放入彈出框里顯示,而彈出框關閉后又把div放回至原理的body中。而且還能拿到文本框的值。
彈出框打開時,如下圖: