0

如何使用 jquery 在多选下拉列表中设置值。我正在使用 MVC3 Razor 视图来显示多选下拉列表并使用http://quasipartikel.at/multiselect_next/这个多选插件。

 <div class="editor-field">         
   @Html.DropDownListFor(model => model.ActionId, (SelectList)ViewBag.ActionsList, new { @class = "multiselect", @multiple = "multiple", @style = "width: 450px;height:300px" })
    @Html.ValidationMessageFor(model => model.ActionId)   
</div>
<button onclick="setValues()">Set values </button>

在javascript中我使用了代码

function setValues() {      
    var valArr = [2, 3, 5];
    var i = 0, size = valArr.length;
    var  $options = $('#ActionId option');
    for (i; i < size; i++) {           
        // $("#ActionId option[value='" + valArr[i] + "']").attr("selected", 1); this is also not working
        $("#ActionId option[value='" + valArr[i] + "']").attr("selected", true);
    }     
}

但是在按钮的单击事件中没有选择这些值。当我刷新页面时,值被选中。我怎样才能克服这个?我想使用javascript在按钮单击时设置multdropdown列表中的值

4

2 回答 2

0

您可以使用选择的 js 进行多选功能。

请参考本文档和演示。

http://harvesthq.github.io/chosen/

于 2013-10-14T05:46:08.573 回答
0

您应该提供字段名称,其值将由 Razor 引擎绑定到下拉列表,因为您需要在下拉列表中提供要绑定的属性名称。试试这个

@Html.DropDownListFor(model => model.ActionId, new SelectList(@ViewBag.ActionsList,"AccountID","AccountName"), new { @class = "multiselect", @multiple = "multiple", @style = "width: 450px;height:300px" })

其中 AccountId,AccountName 是 ActionsList 中的属性字段,AccountName 值将显示在页面中,AccountId 将在选择时绑定

在您的情况下,检查 ActionsList 并将存在的属性名称代替 AccountId 和 AccountName

于 2013-10-11T11:16:18.157 回答