开发者

Update selected values in Multi select with Jquery?

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());
        });
     $开发者_Go百科('#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.


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();
});


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜