[英]Uncaught ReferenceError: onchange function is not defined (wordpress)
I am trying to insert a dropdown list of layers to display the layer selected in a map in wordpress using Openlayers tool. 我正在尝试插入图层的下拉列表,以显示使用Openlayers工具在wordpress中的地图中选择的图层。 But i get this error when i tried to choose from the drpdown list :
但是当我尝试从drpdown列表中进行选择时,出现此错误:
Uncaught ReferenceError: update is not defined
at HTMLSelectElement.onchange ((index):226)
My script : 我的剧本:
var geojsonFormat = new ol.format.GeoJSON();
var vectorSource = new ol.source.Vector({
loader: function(extent, resolution, projection) {
var url = "http://localhost:8080/geoserver/opengeo/ows?service=WFS&" +
"version=2.0.0&request=GetFeature&typename=opengeo:arbousiers0&CQL_FILTER=code_espec='Arteherb'"+
"&outputFormat=text/javascript&format_options=callback:loadFeatures"+
"&srsname=EPSG:3857";
$.ajax({url: url, dataType: 'jsonp', jsonp: false});
},
strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
maxZoom: 19
}))
});
window.loadFeatures = function(response) {
vectorSource.addFeatures(geojsonFormat.readFeatures(response));
};
function update() {
vectorSource.clear(true);
};
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++ ) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
var map = new ol.Map({
layers: [
new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0, 0, 255, 0.0)',
width: 0.3
}),
fill : new ol.style.Fill({
color: getRandomColor()
})
})
})
],
target: document.getElementById('mapid'),
view: new ol.View({
center: [-1095791.453557, 3422374.879112],
maxZoom: 19,
zoom: 5
})
});
my dropdownlist code : 我的下拉列表代码:
<select class='Nom' name="pam" id="pam" onchange="update()">
<option value="">--- Select Layer ---</option>
[insert_php]
$conn_string = "host=localhost port=5432 dbname=postgres user=postgres password=";
$conn = pg_connect($conn_string);
$sql = pg_query($conn, "SELECT * FROM especes");
while($ligne_liste=pg_fetch_array($sql)) {
echo '<option value="'.$ligne_liste['nom'].'">'.$ligne_liste['nom']."</option>\n";
}
echo '</select>';
[/insert_php]
and then i followed the answer here to call the onchage event in functions.php
: https://wordpress.stackexchange.com/questions/248026/add-onchange-to-select-in-a-wp-form 然后我按照此处的答案在
functions.php
调用onchage事件: https ://wordpress.stackexchange.com/questions/248026/add-onchange-to-select-in-a-wp-form
function addAttributeToDropdown($html_content){
$html_content = str_replace('<select','<select onchange="update()"',$html_content);
return $html_content;
}
add_filter('wp_dropdown_cats','addAttributeToDropdown');
It is possible that wordpress might be running the script as an IIFE. WordPress可能会将脚本作为IIFE运行。 So, attach your event handler to the window object
因此,将事件处理程序附加到window对象
window.update = function() {
vectorSource.clear(true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.