2016-12-12 77 views
1

這裏我想創建json格式的數組,我做了所有事情但文本字段保證我想使用選項選擇,位,我不能使數組的json格式,我花了1個多小時了,但我不能得到解決如何在選項中選擇json格式創建數組

function createJSON() { 
 
    result = []; 
 
    $("input[class=email]").each(function() { 
 
\t \t var email = $(this).val(); 
 
\t \t \t var items = []; 
 
\t \t \t $('.Check1:checked').each(function(){ 
 
\t \t \t \t items.push($(this).val()); 
 
\t \t \t \t }); 
 
\t \t $("input[class=mobile]").each(function() { 
 
\t \t \t var mobile = $(this).val(); 
 
\t \t 
 
\t \t result.push({email: email,mobile: mobile, multiselect: items}) 
 
\t \t }); 
 
    }); 
 
    console.log(result); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<form method="POST"> 
 
Email<input title="Title1" type="text" class="email" > 
 
    <input title="Title2" type="text" class="mobile"> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Office Space"> Office Space \t \t \t \t \t \t \t \t 
 
\t </li> 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Apartment"> Apartment \t \t \t \t \t \t \t \t 
 
\t </li> 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Villament"> Villament \t \t \t \t \t \t \t \t 
 
\t </li> 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Individual House"> Individual House \t \t \t \t \t \t \t 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Row House"> Row House 
 
</div> 
 

 

 
<input type="button" id="btn-submit" value="submit" onClick="createJSON()"> 
 
</form>

+0

這是我的工作例如一個例子嗎? –

回答

0

如果我深知,你要這樣呢?

在這裏,我讓使用select multiple.

function createJSON() { 
 
    result = []; 
 
    items=[]; 
 
    $('.Check1:checked').each(function(){ 
 
\t \t items.push($(this).val()); 
 
    }); 
 

 
    //var mobile = $(this).val(); 
 
    result.push({select:$('select').val(), multiselect:items}); 
 
    
 
    console.log(result); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<form method="POST"> 
 
<select multiple="multiple"> 
 
    <option value="option1">1</option> 
 
    <option value="option2">2</option> 
 
    <option value="option3">3</option> 
 
    <option value="option4">4</option> 
 
</select> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Office Space"> Office Space \t \t \t \t \t \t \t \t 
 
\t </li> 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Apartment"> Apartment \t \t \t \t \t \t \t \t 
 
\t </li> 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Villament"> Villament \t \t \t \t \t \t \t \t 
 
\t </li> 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Individual House"> Individual House \t \t \t \t \t \t \t 
 
</div> 
 

 
<div class="col-md-4" style="padding-left: 4px; padding-right: 4px;"> 
 
\t <li style="color: #333; font-size: 85%;"> 
 
\t <input type="checkbox" class="Check1" name="listing_type[]" value="Row House"> Row House 
 
</div> 
 

 

 
<input type="button" id="btn-submit" value="submit" onClick="createJSON()"> 
 
</form>

相關問題