简体   繁体   中英

how to bind array of javascript to mvc4 razor listbox

I have a array in java script

var aaa = ["school1,100"],["school2,101"],["school3,103"]

I want to bind this array to Multiselect Listbox of razor page.

Any one can help me ?

You have to use jQuery for binding Javascript array to the listbox. Below code will help you to bind JS Array to listbox.

JAVASCRIPT

   $(document).ready(function () {

 var aaa = ["school1,100", "school2,101", "school3,103"]

 for (var i = 0; i < aaa.length; i++) {
     $('#listbox').append('<option>' + aaa[i] + '</option>');
 }
});

HTML

 <select id="listbox" size="5"></select>
<select id="listbox" size="5"></select>

JS:

var select = document.getElementById('listbox');
addOptions(select, aaa);

function addOptions(el, ar){
    for(var i = 0; i < ar.length; i++){
        var option = document.createElement('option');
        option.innerHTML = ar[i];
        el.appendChild(option);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM