我正在使用jquery,我想从javascript设置选定的值
使用Javascript/jQuery的
$(document).ready(function() {
var selectedUserRole = document.getElementById('SelectedUserRole').value;
var str_array = selectedUserRole.split(',');
for (var i = 0; i < str_array.length; i++) {
// Trim the excess whitespace.
str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
// Add additional code here, such as:
$("UserRole").prop('selectedValue', str_array[i]); // Here not working
}
});
Run Code Online (Sandbox Code Playgroud)
HTML:
@Html.DropDownList("UserRole", null, new { id = "UserRole", @class = "chosen-select", @multiple = "multiple", @placeholder = "Lütfen Rol Seçiniz", @style = "width: 250px;" })
Run Code Online (Sandbox Code Playgroud)
str_array的值如下
[5,1,7]
我想设置5个值,选择1个值,选择7个值
当我尝试下面
$("UserRole").prop('selectedValue', str_array[i]);
Run Code Online (Sandbox Code Playgroud)
它不适合我.