2016-12-12 42 views
0

在這裏,我想要做的創建多選複選框值數組,如果用戶選擇推出準備搬家意味着我想利用這個值,並通過AJAX數組形式將此值發送到下一個頁面URL,在這裏我把該值,但不能創建陣列格式,如何在jQuery的

$("#fullSearch_btn").click(function(){ 
 
    var items = []; 
 
    $('#property_status:checked').each(function(){ items.push($(this).val()); }); 
 
    var property_status_result = items.join(', '); 
 
    alert(items); 
 
\t \t 
 
}); 
 

 
$.ajax({ 
 
    type:'GET', 
 
     url:"http://domain.com/api/get/areas/city?cityId="+city_id, 
 
     success: function(data) { 
 
      //console.log(data); 
 
     }, 
 
     error:function(exception){ 
 
     alert('Exeption:'+exception); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> \t 
 
<div class="button-group"> 
 
    <button type="button" class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" style="width:100%;text-align:left; border-radius: 0px;">Property Status <span class="caret" style="text-align:right;margin-left: 50px;"></span></button> 
 
    <ul class="dropdown-menu"> 
 
     <li><a href="#" class="small" data-value="option1" tabIndex="-1"><input type="checkbox" id="property_status" name="property_status[]" value="Launched" />&nbsp;Launched</a></li> 
 
     <li><a href="#" class="small" data-value="option1" tabIndex="-1"><input type="checkbox" id="property_status" name="property_status[]" value="Ready To move" />&nbsp;Ready To move</a></li> 
 
     <li><a href="#" class="small" data-value="option1" tabIndex="-1"><input type="checkbox" id="property_status" name="property_status[]" value="Under Construction" />&nbsp;Under Construction</a></li> 
 
     <input type="submit" name="submit" value="submit" id="fullSearch_btn"> 
 
</div> 

+3

首先,ID在文檔上下文中必須是唯一的 –

+0

使用類名代替ID,並且還應該在錨標籤外使用複選框標籤。 –

回答

0

使用分割功能,而不是()join()方法

var property_status_result = items.split(','); 
+0

我需要像這樣[{「Launched」:「準備移動」}] –

+0

如果你想這樣「[」Launched「,」Ready To Move「,」Under Construction「]」你可以使用'var property_status_result = JSON.stringify(items.split( '')); ' – Abhay

0

就像評論說的那樣,改變你的ID。至於發送陣列,你可以這樣做:

var items = []; 
$('.dropdown-menu input').each(function(){ 
    if($(this).is(':checked')) { 
    items.push($(this).val()); 
    } 
}); 
var property_status_result = items.join(', '); 
alert(items); 

但是,這將創建一個數組項目的字符串。那是你需要的嗎?

+0

我需要像這樣[{「Launched」:「準備移動」}] –

+0

這是一個數組內的對象。第一個項目需要成爲一個關鍵字,更有可能你需要像這樣'[{value:「Launched」},{value:「Ready To move」}]'。但是這不能在URL參數中發送。也許用'data'屬性發送它? 另外,如果你需要你的表單,你可以用一個if來做,所以它可以看起來像這樣[[{Launched:「Ready To move」}]'。 你能給我一個API解釋什麼數據的例子嗎?也許我可以這樣解決:) – yomisimie

+0

我正在做財產搜索,他們返回像這樣的值[{ 「createdBy」:「agent」, 「createdDate」:「2016-09-20T08:13:30.418Z」 , 「property_status」:[已啓動,「準備移動」, } ] –