JQuery multiselect - 在多选下拉列表中设置一个选定的值

时间:2011-08-06 10:43:33

标签: jquery jquery-multiselect

我有一个多选下拉列表,如下所示

<select id="data" name="data" class="data" multiple="multiple">
  <option value="100">foo</option>
  <option value="101">bar</option>
  <option value="102">bat</option>
  <option value="103">baz</option>
</select>

在加载页面时,我将得到一个像[101,102]这样的值数组。 我应该遍历数组并选择值(应检查对应于id的复选框)。 请帮忙。

17 个答案:

答案 0 :(得分:139)

//Do it simple

var data="1,2,3,4";

//Make an array

var dataarray=data.split(",");

// Set the value

$("#multiselectbox").val(dataarray);

// Then refresh

$("#multiselectbox").multiselect("refresh");

答案 1 :(得分:22)

谢谢大家的回答。

以您的所有答案为准则,我使用以下代码解决了问题:

var valArr = [101,102];
i = 0, size = valArr.length;
for(i; i < size; i++){
  $("#data").multiselect("widget").find(":checkbox[value='"+valArr[i]+"']").attr("checked","checked");
  $("#data option[value='" + valArr[i] + "']").attr("selected", 1);
  $("#data").multiselect("refresh");
}

再次感谢您的支持。

答案 2 :(得分:18)

它应该像这样简单:

<select id='multipleSelect' multiple='multiple'>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
<script type='text/javascript'>
    $('#multipleSelect').val(['1', '2']);
</script>

检查我的小提琴:https://jsfiddle.net/luthrayatin/jaLygLzo/

答案 3 :(得分:10)

var valArr = [101,102], // array of option values
    i = 0, size = valArr.length, // index and array size declared here to avoid overhead
    $options = $('#data option'); // options cached here to avoid overhead of fetching inside loop

// run the loop only for the given values
for(i; i < size; i++){
    // filter the options with the specific value and select them
    $options.filter('[value="'+valArr[i]+'"]').prop('selected', true);
}

Here is a demo

答案 4 :(得分:4)

该代码应该有所帮助:

var selected=[101,103];
var obj=$('#data');
for (var i in selected) {
    var val=selected[i];
    console.log(val);
   obj.find('option:[value='+val+']').attr('selected',1);
}

答案 5 :(得分:3)

从php数组进入multiselect ...

$PHP_array=array();           // create array anyway you need to in PHP
array_push($PHP_array,20);    // Single ID values
array_push($PHP_array,22);

$javascript_str = json_encode($PHP_array);   // JSON ENCODE        

//然后在JS脚本中

$("#multiselectbox").val(<?php echo $javascript_str; ?>);

// Then refresh

$("#multiselectbox").multiselect("refresh");

答案 6 :(得分:3)

适合我,根据您的要求更改ID。

$("#FormId select#status_id_new").val('');
 $("#FormId select#status_id_new").multiselect("refresh");

答案 7 :(得分:2)

official site已提及(在&#34;手动选中或取消选中复选框?&#34;部分)只是为了使用click()

e.g。

$("#ddl_singleSelect")
    .multiselect("widget")
        .find(":radio[value='']").click();  // change displayed selectedText
$("#ddl_singleSelect option[value='']")
    .prop("selected", true)             // change val()
    .trigger("change");                 // fire change event


$("#ddl_multiselect").multiselect("uncheckAll");    // init by de-selecting all
$("#ddl_multiselect")
    .multiselect("widget")
        .find(     ":checkbox[value='1']"
                + ",:checkbox[value='2']"
                + ",:checkbox[value='3']"
                + ",:checkbox[value='4']"
            ).click();  // change displayed selecetdText
$("#ddl_multiselect")
    .find(     "option[value='1']"
            + ",option[value='2']"
            + ",option[value='3']"
            + ",option[value='4']"
        ).prop("selected", true);   // change val()
$("#ddl_multiselect").trigger("change");    // fire change event

HTML

<select name="example-presets" multiple="multiple" size="5">
    <option value="option1" selected="selected">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3" selected="selected">Option 3</option>
    <option value="option4" selected="selected">Option 4</option>
    <option value="option5" disabled="disabled">Option 5</option>
    <option value="option6" disabled="disabled">Option 6</option>
    <option value="option7">Option 7</option>
    <option value="option8">Option 8</option>
    <option value="option9">Option 9</option>
</select>
....
<script type="text/javascript">$("select").multiselect();</script>

答案 8 :(得分:2)

 var valArr = ["1","2","3"];

/* Below Code Matches current object's (i.e. option) value with the array values */
 /* Returns -1 if match not found */
 $("select").multiselect("widget").find(":checkbox").each(function(){
    if(jQuery.inArray(this.value, valArr) !=-1)
            this.click();

   });

这将选择其值与数组中的值匹配的选项。

答案 9 :(得分:2)

我从jquery-multiselect documentation获得了更好的解决方案。

var data = [101,102];

$("#data").multiSelect('deselect_all');

$("#data").multiSelect("select",data);

答案 10 :(得分:0)

这就是我使用数组作为输入的方法。首先取消选中所有选项,然后使用jquery

单击所选选项
var dimensions = ["Dates","Campaigns","Placements"];

$(".input-dimensions").multiselect("uncheckAll");

for( var dim in dimensions) {
  $(".input-dimensions").multiselect("widget")
    .find(":checkbox[value='" + dimensions[dim] + "']").click();
}

答案 11 :(得分:0)

这是我的代码示例。 对于多选下拉列表,我使用逗号分隔数字,并希望从逗号分隔值中选择多个选项。

var selected_tags_arr = new Array();
var selected_tags = 1,2,4;
selected_tags_arr = selected_tags.split(",");
$('#contact_tags option').each(function (){
    var option_val = this.value;
    for (var i in selected_tags_arr) {
        if(selected_tags_arr[i] == option_val){
            $("#contact_tags option[value='" + this.value + "']").attr("selected", 1);
        }
    }
});

答案 12 :(得分:0)

我最后不得不使用输入元素上的click事件进行轻微更改,同时在触发click事件后设置检查过的prop。

$(y.Ctrl).multiselect("widget").find(":checkbox").each(function () {
    if ($.inArray(this.value, uniqueVals) != -1) {
        $(this).click();
        $(this).prop('checked', true);
    }
});

答案 13 :(得分:0)

$("#dropDownId").val(["130860","130858"]);
$("#dropDownId").selectmenu('refresh');

答案 14 :(得分:0)

希望这些功能对您有所帮助。

$('#your-select')。multiSelect('select',String | Array);

$('#your-select')。multiSelect('deselect',String | Array);

$('#your-select')。multiSelect('select_all');

$('#your-select')。multiSelect('deselect_all');

$('#your-select')。multiSelect('refresh');

此网站的参考  参考

答案 15 :(得分:0)

按值选择选项。也可以使用值数组。 查找完整详情http://davidstutz.github.io/bootstrap-multiselect/#methods

$('#example-select').multiselect('select', ['1', '2', '4']); 

答案 16 :(得分:-2)

你可以试试这个:

class MyApplication extends Application{
public static ArrayList<MyModel> myModelList = new ArrayList<>();
}

class MyModel{
public boolean isLikeSelected;
}
相关问题