JQuery mobile - how to set the values of a multiple select?

末鹿安然 提交于 2019-12-06 07:51:05

问题


I have a multiple select in jquery mobile.

docs: http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-selects.html

To get the value I do this

    var leerplandoelen = $("#AddLessonForm_leerplandoelen").val();

        var leerplandoelenString = "";
        if(leerplandoelen != null){
            $.each(leerplandoelen, function(i, le){
                if(i!=leerplandoelen.length-1){
                    leerplandoelenString += "\""+le+"\",";
                }else{
                    leerplandoelenString += "\""+le+"\"";
                }
            });
        }

To get a string like this '"1", "2", "3"'.

How can I set the values?

I tried the following:

    $('#AddLessonForm_leerplandoelen').val(['3','6','14']).selectmenu('refresh');

But it didn't work.


回答1:


Do it this way, read array of selected items. Mark option selected based on value

// array of values
var selected = ["2", "4"];

$.each(selected, function (i, v) {

  // mark options selected
  $("select option[value='" + v + "']").prop("selected", true);

  // refresh selectmenu
  $("select").selectmenu("refresh");

});

Demo



来源:https://stackoverflow.com/questions/20494875/jquery-mobile-how-to-set-the-values-of-a-multiple-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!