我有兩個文件,一個JSON存儲一些數據和一個簡單的html表單。如何使用JSON數據填充表單
我想用從JSON中獲取的隨機數據填充表單。這些數據應該是相對於同一個對象。
我的形式看起來像:
<form id="first_form">
Name:<br>
<input id="name" type="text" name="name">
<br>
Phone:<br>
<input id="phone" type="number" name="phone">
<br>
City:<br>
<input id="city" type="text" name="city">
<br>
Pcode:<br>
<input id="pcode" type="number" name="pcode">
Note:<br>
<input id="note" type="text" name="note">
<br>
</form>
我的JSON的樣子:
users: [
{
"name": "name1",
"phone": "111111111",
"address": "address1",
"city": "city1",
"pcap": 1111,
"note": ""
},
{
"name": "name2",
"phone": "222222222",
"address": "address2",
"city": "city2",
"pcap": 2222,
"note": ""
},
{
"name": "name3",
"phone": "333333333",
"address": "address3",
"city": "city3",
"pcap": 3333,
"note": ""
},
{
"name": "name4",
"phone": "44444444",
"address": "address4",
"city": "city4",
"pcap": 4444,
"note": ""
}
]
有沒有辦法在一個沒有jQuery的方式來做到這一點?
你有什麼已經嘗試過這種情況?除此之外,如果它包含'users:{'那麼它不是JSON編碼的數據,而是一個JavaScript對象。因爲在JSON格式的數據中,鍵必須帶有雙引號。 –