Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all selected values using Bootstrap Multiselect and use it in AJAX

I am using Bootstrap Multiselect, this is my setup in my HTML:

<div class="form-group">
    <select class="form-control" id="bldg_type" multiple="multiple">{% for type in buildings %}
        <option value="{{type.bldg_type}}">{{type.bldg_type}}</option>{% endfor %}
    </select>
</div>

I wanted to use the selected values for my query, the snippet of my Ajax code:

$.ajax({
    url: "cnt_bldg/",
    type: "GET",
    dataType: "JSON",
    data: {
        'brgy_id': brgy_id,
        'bldg_type': $('#bldg_type option:selected').val()
    },
    ...

The problem with $('#bldg_type option:selected').val() is I can only get 1 value. For e.g. I check Market and Police Station in my select option, and looked it in my console http://127.0.0.1:8000/cnt_bldg/?brgy_id=All&bldg_type=Market". It only gets the Market.

How do I get all the values and use it for my query?

BTW, I'm using Django. I have read this answer already, but I don't know how to use the result in my AJAX code above.

like image 611
Sachi Tekina Avatar asked Apr 20 '15 03:04

Sachi Tekina


2 Answers

have you tried simple

$('#bldg_type').val()

It works in my bootstrap-multiselect.

like image 178
kite Avatar answered Nov 15 '22 11:11

kite


HTML

<div class="form-group">
        <select id="multiselect1" class="form-control" id="bldg_type" multiple="multiple">{% for type in buildings %}
            <option value="{{type.bldg_type}}">{{type.bldg_type}}</option>{% endfor %}
        </select>
</div> 

Jquery

var selected = $('#multiselect1').val()
like image 29
Afsal KK Avatar answered Nov 15 '22 11:11

Afsal KK