在頁面中使用拼接字串的方式顯示動態載入的資料

來源:互聯網
上載者:User

標籤:調用   mdi   前台   userinfo   emd   arc   pos   idt   success   

在做頁面的時候為了使頁面美化,我們經常會用拼接字串的方式,動態載入背景資料,這裡我們使用的前台架構是bootstrap,但是很多效果還是要用jquery來實現

(1)方法傳參與字串的拼接  (拼接用的replace)

         先聲明一個展示資料的頁面模型(使用過的模型1)

var userModel = "<div class=\"itemdiv commentdiv\" style=\"margin-left: 7%;\">"+
"<div class=\"row text-left\" style=\"margin-top:5px;\">"+
"<label style=\"color: #737D81;\">使用者ID:</label>"+
"<label>&nbsp;&nbsp;{userid}</label>"+
"</div>"+
"<div class=\"row text-left\" style=\"margin-top:5px;\">"+
"<label style=\"color: #737D81;\">姓名:</label>"+
"<label>&nbsp;&nbsp;{username}</label>"+
"<button id=\"{btnId}\" class=\"btn btn-sm btn-info\" style=\"height:30px;margin-left: 20%;\">添加</button>"+
"</div>"+
"<div class=\"row text-left\" style=\"margin-top:5px;\">"+
"<label style=\"color: #737D81;\">電話:</label>"+
"<label>&nbsp;&nbsp;{telephone}</label>"+
"</div>"+
"<div class=\"row\">"+
"<div class=\"hr hr8\"></div>"+
"</div>"+
"</div>";

        然後ajax載入後台資料,然後使用replace,將資料動態替換到頁面模型中

$.ajax({
url:‘<%=basePath%>platform/getUserPad.action‘,
type : ‘POST‘,
dataType : ‘json‘,
data :{
user_number : $("#form_searchSecurity").val(),
user_name : $("#search_user_name").val(),
},
success : function(data) {


$.each(data, function(index, element) {

    //此處替換頁面模型中的值
var oneUserInfo = userModel.replace(/{userid}/g,element.userid).replace(/{username}/g,element.username)
.replace(/{telephone}/g,element.telephone)
.replace(/{btnId}/g,‘btnId‘+index);         //給button設定動態ID,不需要可不設定


$("#userModelDiv").append(oneUserInfo);   //將頁面中的值替換好之後,將這些資料插入頁面中需要放這些資料處的div中


document.getElementById(‘btnId‘+index).onclick = function() {         //動態添加事件,若在每條資料的後面要添加件事傳參,此方式可傳一個對象過去
addNumberBySearch(element);                        //調用這個方法,把對象傳過去,

}

 

//或者是在頁面中定義方法,此處傳不同參數,同樣使用replace方法

 

});

}
});

 

function addNumberBySearch(element){
//在此方法中添加操作
element.userid          //擷取屬性值
}

      拼接字串的時候,遇到傳值的問題,可選擇方法傳值(可以在button裡面onclick=aa(參數1,參數2))

 

 

 

 

 

 

 

 

   

 

在頁面中使用拼接字串的方式顯示動態載入的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.