tags:

views:

66

answers:

2

This is my function to check which checkboxs are checked.

function updateTagChecked() {
         var list_tag_selected = [];//to store the value of checkboxs selected.
     $('input[name=tag]:checked').each(function() {
         list_tag_selected.push($(this).val());
        });
     $('#tag_checked').val(list_tag_selected.join("&"))//Append this list to hidden field

    }
    //When are checkbox are click?
    $(function() {
         $('#collapse_option input').click(updateTagChecked);
         updateTagChecked();
     });

I want to do the same thing for multi select

<div id collapse_option>
    <li><label for="id_city">City:</label> 
   <select multiple="multiple" name="city" id="id_city">
    <option value="1">Phnom Penh</option>
    <option value="2">Takeo</option>
    <option value="3">Kampot</option>
    <option value="4">Kampongthom</option>
    <option value="5">Siemreip</option>
    <option value="6">pursat</option>
    <option value="7">preyveng</option>
    </select></li>
  </div>
<input type="hidden" id="store_multiselected" name="store_multiselected" value=""/>

In multi select Can we do this.?I want to store value for every selected in multi select in hidden field as I mention above.

+1  A: 

Yes, just use the selector select[name=city] option[selected] instead of input[name=tag]:checked.

Joel Potter
you mean Should I change to or some thing else?$('input[name=tag]:checked') =$('select[name=city] option[selected]'
python
Thank for your tips.It works
python
+1  A: 

When its a multiselect, the val() is already a list:

function updateTagChecked() {
    var list_tag_selected = $('#id_city').val();     // first pull values from multiselect
    $('input[name=tag]:checked').each(function() {
        list_tag_selected.push($(this).val());       // then from other checkboxes
    });

    //Append this list to hidden field
    $('#tag_checked').val(list_tag_selected.join("&"));
}

//When are checkbox are click?
$(function() {
     $('#collapse_option input').click(updateTagChecked);
     updateTagChecked();
});
Rob Van Dam
var list_tag_selected = $('#id_city').val(); // first pull values from multiselectThanks for this point.:)
python