How do I pass a model array into jquery?
I have an array from a model that I would like to use in my jquery script. How do I do that?
Model.list
<select id='ddlchanger' onchange="toggleChange(/* pass array here*/)"></select>
function toggleChange(list) {
for (i = 0; i < list.le开发者_如何学Gongth; i++) {
$('<option/>').val(list[i]).html(list[i]).appendTo('#drpdownSub');
}
}
Rather than binding the on change event in your html, bind it in the javascript, then you can pass in the array.
$(function(){
var Model.list;
$('#ddlchanger').change(function(){
toggleChange(Model.list);
});
function toggleChange(list) {
for (i = 0; i < list.length; i++) {
$('<option/>').val(list[i]).html(list[i]).appendTo('#drpdownSub');
}
}
});
I would attach the event handler with jQuery:
var data = [...];
function toggleChange(list) {
for (i = 0; i < list.length; i++) {
$('<option/>').val(list[i]).html(list[i]).appendTo('#drpdownSub');
}
}
$(function() {
$('#ddlchanger').change(function() {
toggleChange(data);
});
});
which leaves the HTML as
<select id='ddlchanger'>
...
</select>
精彩评论