Select change (not with "$(this).attr('value'))" but to trigger execution of another function - jQuery
I want trigger execution of another function from select change.
I want to trigger execution of updateFormats(); How to do this in jQuery?
$('#inproj').change(function(){
updateFormats();
});
$('#outproj').change(function(){
updateFormats();
});
The code below works for changing the value of all select. However, I want the inproj and outproj select to trigger the execution of updateFormats() mentioned above.
$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str += $(this).text() + " ";
});
$("div").text(str);
})
.change();
The HTML of select inproj and outproj (jquery-mobile);
<div data-role="fieldcontain">
<label for="inproj" class="select">Input Projection:</label>
<select name ="inproj" id="inproj">
<option value="EPSG:4326" selected="selected">EPSG:4326</option>
<option value="EPSG:900913">Spherical Mercator</option>
</select> <br />
</div>
<div data-role="fieldcontain">
<label for="outroj" class="select">Output Projection:</label>
<select name="outproj" id="outproj">
<option value="EPSG:4326" selected="selected">EPSG:4326</option>
<option value="EPSG:900913">Spherical Mercator</option>
</select>
<br />
</div>
The updateFormats() function.
$(function() {
function updateFormats() {
var in_options = {
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("inproj").value)
};
var out_options = {
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("outproj").value)
};
var gmlOptions = {
featureType: "feature",
featureNS: "http://example.com/feature"
};
var gmlOptionsIn = OpenLayers.Util.extend(
OpenLayers.Util.extend({}, gmlOptions),
in_options
);
var gmlOptionsOut = OpenLayers.Util.extend(
OpenLayers.Util.extend({}, gmlOptions开发者_StackOverflow),
out_options
);
var kmlOptionsIn = OpenLayers.Util.extend(
{extractStyles: true}, in_options);
formats = {
'in': {
wkt: new OpenLayers.Format.WKT(in_options),
geojson: new OpenLayers.Format.GeoJSON(in_options),
georss: new OpenLayers.Format.GeoRSS(in_options),
gml2: new OpenLayers.Format.GML.v2(gmlOptionsIn),
gml3: new OpenLayers.Format.GML.v3(gmlOptionsIn),
kml: new OpenLayers.Format.KML(kmlOptionsIn),
atom: new OpenLayers.Format.Atom(in_options)
},
'out': {
wkt: new OpenLayers.Format.WKT(out_options),
geojson: new OpenLayers.Format.GeoJSON(out_options),
georss: new OpenLayers.Format.GeoRSS(out_options),
gml2: new OpenLayers.Format.GML.v2(gmlOptionsOut),
gml3: new OpenLayers.Format.GML.v3(gmlOptionsOut),
kml: new OpenLayers.Format.KML(out_options),
atom: new OpenLayers.Format.Atom(out_options)
}
};
}
});
});
});
you mean like:
$('#inproj, #outproj').change(updateFormats);
that should trigger the function but you've put your function inside a $(function(){...})
block which seems odd.
so try
$(function(){
function updateFormats() {
// updateFormats code here
};
$('#inproj, #outproj').change(updateFormats);
});
let me know any problems.
Update
here is an example of it working in jsFiddle
精彩评论