2017-01-28 85 views
0

我有一個數組,arr。我想爲陣列中的每個對象追加按鈕#enquirieswall。我嘗試了各種方法,但迄今爲止還沒有工作。這就是我想出迄今:如何在每個循環迭代中附加一個按鈕?

<script> 
var arr = [{"userid": "jim", "title":"help"}, {"userid": "pim", "title":"hello"}] 

var i; 
$("enquirieswall").empty(); 
for (i=0; i < arr.length; i++){ 
    var a = document.createElement("a"); 
    a.setAttribute("href","#"); 
    a.setAttribute("class","ui-btn"); 
    //added according to comments on my question 
    a.textContent = arr[i].title; 
    a.setAttribute("onclick", "window.location='enquiriesdetails.php?userid=" + arr[i].userid +"'"); 
    $("#enquirieswall").append(a); 
} 
</script> 

<div id="enquirieswall"></div> 

目前的結果是相互聯繫並排,我想他們是在按鈕,而不是。

+1

你'了'元素沒有任何textContents? – undefined

+2

數組中的語法錯誤,缺少','且沒有內容顯示錨元素 - https://jsfiddle.net/arunpjohny/6ppj8euq/2/ –

+1

或https://jsfiddle.net/arunpjohny/6ppj8euq/ 3/ –

回答

0

改變的createElement到按鈕和UI-BTN到BTN

for (i = 0; i < arr.length; i++) { 
         var a = document.createElement("button"); 
         a.setAttribute("href", "#"); 
         //a.textContent = arr[i].title; 
         a.innerHTML = arr[i].title.toString(); 
         a.setAttribute("class", "btn"); 
         a.setAttribute("type", "button"); 
         a.setAttribute("onclick", "window.location='enquiriesdetails.php?userid=" + arr[i].userid + "'"); 
         $("#enquirieswall").append(a); 
        } 
相關問題