Ich habe einen jQuery-Skript geschrieben, die gleiche Aufgabe tut, sondern auf verschiedene Ereignisse sagenjQuery Skripte Kombination, um es kleinere
- Auf Seite Last
- Bei Änderung der Drop-Down-Auswahl
- Bei Klick auf Swap-Taste
- auf keyup in einem Textfeld
Aber ich hatte die Skripte für alle von ihnen getrennt zu schreiben. Ich bin neu bei jQuery. Ich möchte die Scripts kombinieren, um sie gleichzeitig zu verwenden, sie aber kleiner zu machen. Ist es möglich?
Script
<script type="text/javascript">
// Fire this function when page loads
$(document).ready(function() {
$.getJSON(
'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
function(data){
if(typeof fx !== "undefined" && fx.rates){
fx.rates = data.rates;
fx.base = data.base;
var amount = $("#amt").val();
var from = $("#from").val();
var to = $("#to").val();
$("#res").val(fx(amount).from(from).to(to));
$("#result").show();
}
}
);
});
// Fire this function when value is entered in the field
$(document).keyup('#amt', function(){
$.getJSON(
'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
function(data){
if(typeof fx !== "undefined" && fx.rates){
fx.rates = data.rates;
fx.base = data.base;
var amount = $("#amt").val();
var from = $("#from").val();
var to = $("#to").val();
$("#res").val(fx(amount).from(from).to(to));
$("#result").show();
}
}
);
});
// Fire this function on swap button click
$("#swap").click(function(e) {
e.preventDefault();
var fromVal = $("#from option:selected").val();
var fromText = $("#from option:selected").text();
var toVal = $("#to option:selected").val();
var toText = $("#to option:selected").text();
$("#from option:selected").val(toVal);
$("#from option:selected").text(toText);
$("#to option:selected").val(fromVal);
$("#to option:selected").text(fromText);
$.getJSON(
'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
function(data){
if(typeof fx !== "undefined" && fx.rates){
fx.rates = data.rates;
fx.base = data.base;
var amount = $("#amt").val();
var from = $("#from").val();
var to = $("#to").val();
$("#res").val(fx(amount).from(from).to(to));
$("#result").show();
}
}
);
});
// Fire this function on change of "FROM" dropdown selection
$("#from").change(function() {
$.getJSON(
'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
function(data){
if(typeof fx !== "undefined" && fx.rates){
fx.rates = data.rates;
fx.base = data.base;
var amount = $("#amt").val();
var from = $("#from").val();
var to = $("#to").val();
$("#res").val(fx(amount).from(from).to(to));
$("#result").show();
}
}
);
});
// Fire this function on change of "TO" dropdown selection
$("#to").change(function() {
$.getJSON(
'https://openexchangerates.org/api/latest.json?app_id=2e946db1e3fc4888b61330e505a804e5',
function(data){
if(typeof fx !== "undefined" && fx.rates){
fx.rates = data.rates;
fx.base = data.base;
var amount = $("#amt").val();
var from = $("#from").val();
var to = $("#to").val();
$("#res").val(fx(amount).from(from).to(to));
$("#result").show();
}
}
);
});
</script>
Sie möchten nicht, dass Ihre API-Schlüssel öffentlich sichtbar sind. Ich schlage vor, Sie entfernen sie von der Frage. – Tomalak