如何在jquery中的multiselect复选框值中创建数组

时间:2016-12-12 12:42:01

标签: php jquery ajax

我想这样做,如果用户选择已启动并且准备移动表示我想获取该值并将此值以数组格式发送到下一页AJAX网址,这里我取值但不能创建数组格式,

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

$.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>	
<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>      

2 个答案:

答案 0 :(得分:0)

使用split函数代替()

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

答案 1 :(得分: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);

但是这会创建一串数组项。这就是你需要的吗?