-1
這裏我有一個表單和一個表單字段,即複選框,在檢查了三個字段之後,我想要製作JSON格式,但是我無法做到這一點,如果任何人都知道請更新我的答案。如何在formfields中使用複選框來製作json數組和對象
function rentfunction(){
var arr1 = [];
\t \t \t $.each($("input[name='furniture_check']:checked"),function(){
\t \t \t var furniture = $(this).val();
\t \t \t arr1.push(furniture);
\t \t \t });
\t \t \t var data = {
\t \t \t \t "rentProperty" :{
\t \t \t \t \t "furnitureType" :arr1,
\t \t \t \t }
\t \t \t }
\t \t \t console.log(data);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form>
\t <input type="checkbox" name="furniture_check" value="Ward robe">Ward robe <br>
\t <input type="checkbox" name="furniture_check" value="Lights">Lights <br>
\t <input type="checkbox" name="furniture_check" value="Fan">Fan <br>
\t <input type="checkbox" name="furniture_check" value="Fridge">Fridge <br><br><br>
<button type="button" id="rentBtnSubmit" onclick="rentfunction()">Submit</button>
</form>
預期的答案
假如我是點擊病房長袍&燈&冰箱,我點擊提交按鈕意味着我要讓JSON這樣
{
"rentProperty":
{
"fullName" : "Some Name"
},
"floorType": [
{
"floorTypeName": "Ward robe"
},
{
"floorTypeName" :"Lights"
},
{
"floorTypeName" :"Fridge"
}
]
}
我也試過,但我不能夠做出預期的JSON格式,這樣
{
"rentProperty": {
"furnitureType": [
"Ward robe",
"Lights",
"Fan"
]
}
}
爲什麼在代碼中生成'furnitureType'時,該屬性無法在您想要的輸出中出現!? – trincot