【问题标题】:JQuery mobile - how to set the values of a multiple select?JQuery mobile - 如何设置多选的值?
【发布时间】:2013-12-10 12:41:15
【问题描述】:

我在 jquery mobile 中有多项选择。

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

为了得到我这样做的价值

    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+"\"";
                }
            });
        }

要得到这样的字符串 '"1", "2", "3"'。

如何设置值?

我尝试了以下方法:

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

但是没有用。

【问题讨论】:

    标签: javascript jquery jquery-mobile multiple-select


    【解决方案1】:

    这样做,读取所选项目的数组。基于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

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      相关资源
      最近更新 更多