"missing ] after element list" with eval
im trying to pass two parameters to a function, "i" being an int value and "map" being an object. I'm using eval to inform Javascript that I am passing objects, however im getting the error "missing ] after element list".
I'm kinda new to this so I'm not 100% sure if this is the right way of doing things... but ya.. hopefully i'm on the right track.
...
optionsControlDiv.innerHTML += '<input type="checkbox" onclick="toggleLayer('+eval(i)+","+eval(map)+')"'+c+' />'+this.opts[i]+'<br>';
console.log('<input type="checkbox" onclick="toggleLayer('+eval(i)+","+eval(map)+')"'+c+' />'+this.opts[i]+'<br>');
// OUTPUT:
// <input type="checkbox" onclick="toggleLayer(0,[object Object])" />Wiki<br>
// <input type="checkbox" onclick="toggleLayer(1,[object Object])" />webcams<br>
// <input type="checkbox" onclick="toggleLayer(2,[object Object])" />Photos<br>
// ERROR:
// missing ] after element list
// [Break on this error] toggleLayer(0,[object Object])
...
// The function
function toggleLayer(i,map) {
//console.log(i);
if (layers[i].Visible) {
layers[i].hide();
} else {
if(layers[i].Added) {
layers[i].sh开发者_如何学Cow();
} else {
map.addOverlay(layers[i]);
layers[i].Added = true;
}
}
layers[i].Visible = !layers[i].Visible;
}
If map
is global variable this should work without using eval:
optionsControlDiv.innerHTML += '<input type="checkbox" onclick="toggleLayer('+i+',map)"'+c+' />'+this.opts[i]+'<br>';
console.log('<input type="checkbox" onclick="toggleLayer('+i+',map)"'+c+' />'+this.opts[i]+'<br>');
You've got an error because eval
expects string as parameter, and map
object while called as string returns [object Object]
which is being evaluated and results in error.
精彩评论