2

My code below to populate multiselect with item value 1 and 2 are default selected but only item value 2 is selected :

$.ajax({
        type: "POST",
        url: Url_function,
        data: para_datas,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function OnPopulateControl(response) {
            list = response.d;
            control.multiselect({
                selectedList: selectedlist,
                header: false,// 0-based index
                selectedList: 2
            });
            var $select = control.multiselect('disable');
            if (list.length > 0) {
                        $select.multiselect('enable');
                        $.each(list, function () {
                            control.append($("<option></option>").val(this['Value']).html(this['Text']));
                            //console.log(this['Text']);
                        });
                    }
                    else {
                        control.empty().append('<option selected="selected" value="0">Not available<option>');
                    }
                a_list_selected = valueselected.split(',');//valueselected = '1,2'
                for (i =0; i< a_list_selected.length; i++)
                {
                      control.val(a_list_selected[i]).attr('selected',true);

                }
                 control.multiselect('refresh'); //refresh the select here
        },
        error: function () {
            alert(Error_message);
        }
    });
Quaternion
  • 10,380
  • 6
  • 51
  • 102
Hainlp
  • 169
  • 1
  • 5
  • 18
  • 1
    What is `control.multiselect`? Or just `control` for that matter? Sounds like a jQuery plugin. – Tejs Sep 14 '11 at 07:02
  • Yes, i use jquery plugin http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/ . Control is $("#sid") in this case – Hainlp Sep 14 '11 at 07:11
  • what version of jquery you are using? if its 1.6 or higher you can use `.prop("selected",true)` or as @Py. said `.attr("selected","selected")` – Rafay Sep 14 '11 at 07:33
  • The version of jquery is 1.4.4 – Hainlp Sep 14 '11 at 07:49
  • Thanks you all for your answers. I resolve the problem with below code : control.find('option[value="'+a_list_selected[i]+'"]').attr('selected', true); – Hainlp Sep 14 '11 at 08:43

1 Answers1

1

From the doc of the plugin.

Manually check or uncheck a checkbox?

The checkboxes can be accessed after calling the "widget" method. Simply manually trigger the NATIVE click event on them:

// manually check (or uncheck) the third checkbox in the menu: $("select").multiselect("widget").find(":checkbox").each(function(){ this.click(); });

The native click event must be used (trigger('click') will not work) due to this bug in jQuery’s core.

All necessary events and actions, like updating the button value, will automatically fire.

Alternatively, you could give the original option tag the selected attribute, and then call MultiSelect’s refresh method.

You've opted for the selected way but in your code, your setting selected to true control.val(a_list_selected[i]).attr('selected',true);. The value to set it is 'selected' like control.val(a_list_selected[i]).attr('selected','selected'); (see http://reference.sitepoint.com/html/option/selected for example for the value it could take)

Py.
  • 3,499
  • 1
  • 34
  • 53
  • I don't understand, please explain it. :) – Hainlp Sep 14 '11 at 07:52
  • have you tried to put `control.val(a_list_selected[i]).attr('selected','selected');` in your code (with 'selected' instead of true)? – Py. Sep 14 '11 at 07:55
  • 'selected' or 'true' are correct, but only one item is selected, i want 2 items are selected – Hainlp Sep 14 '11 at 07:59
  • Are you sure that the items with the value 1 and 2 exist? (the HTML produced by you code would help) – Py. Sep 14 '11 at 08:02
  • Thanks you all for your answers. I resolve the problem with below code : control.find('option[value="'+a_list_selected[i]+'"]').attr('selected', true); – Hainlp Sep 14 '11 at 08:43
  • Then answer your question and accept your answer (well you might not have the right to do that before 48 hours :p) – Py. Sep 14 '11 at 09:16