2015-04-04 60 views
0

在我的項目,我有一個Ajax請求,從數據庫中選擇條目,我選擇是建立在格式化這樣陣列將數據放入相應的輸入表

"storey": [{ 
    "storeyno": "1st Floor", 
    "areastorey": "1" 
}, { 
    "storeyno": "2nd Floor", 
    "areastorey": "2" 
}, { 
    "storeyno": "3rd Floor", 
    "areastorey": "3" 
}, { 
    "storeyno": "4th Floor", 
    "areastorey": "4" 
}, { 
    "storeyno": "5th Floor", 
    "areastorey": "5" 
}, { 
    "storeyno": "6th Floor", 
    "areastorey": "6" 
}], 
的面積和樓層號碼的數據

這兩個數據組合是每個表的行。 Storeyno代表樓層號,它只是一個p,areastoery是一個輸入值,它是樓層的面積。

我的問題是如何將其重新放回到輸入文本中。樓號不是問題(storeyno),因爲我正在動態生成它,問題是將輸入值。

for (var j = 0; j < storeyno.length; j++) { 
    console.log(storeyno); 
    var bldgstorey = $('table#floor').find('tr:not(:first-child,:last-child)'); 
    if (storeyno[j].storeyno !== null) { 
     bldgstorey.each(function() { 
      var floornumber = $(this).find('td p').text(); 
      var floorinput = $(this).find('td input[type=text]').attr('id'); 
      console.log(storeyno[j].areastorey + " = " + floorinput); 
      console.log(floorinput); 
      $(this).find('td input[type=text]').val(storeyno[j].areastorey); 
      //$(this).find('td:nth-child(2)').find('input[type=text]').val(storeyno[j].areastorey); 
     }); 
    } 
} 

注意,從選擇我添加tr:not(:first-child,:last-child)因爲第一行是標題而最後一行是總。

FYI

我動態生成行,但它是不相關的問題,所以我認爲這並不重要

Update

Sample

更新與樣品地板爲樣本樣品數據

Sample with exact same floors

Problem

如何正確放置正確的價值從數據庫中輸入。當我說得好floor 1 input text box will be populated by database that is for floor 1

+0

可以請你提供小提琴? – 2015-04-04 04:23:50

+0

給我一分鐘我會做一個 – 2015-04-04 04:29:54

+0

那麼你的問題是什麼?我仍然沒有得到它。 – 2015-04-04 04:48:05

回答

1

使用下面的代碼。使用jquery each()來獲取數據frmm json。

檢查這個DEMO

$.each(storey,function(key,value){ 
    $('#floor'+(key+1)).val(value.areastorey); 
    });