我有以下 jQuery 代码,其中很大一部分被反复使用。有什么方法可以减少这种情况,以免重复太多?
// first slider
$("#slider").slider({
value: "0",
min: 0,
max: 3,
step: 1,
slide: function(event, ui) {
$("#price").val(t[ui.value]);
$("#amount").val(p[ui.value]);
var aaa = $("#price").val();
var bbb = $("#priceb").val();
var ccc = $("#pricec").val();
var ddd = $("#priced").val();
var eee = $("#pricee").val();
var fff = $("#pricef").val();
var ggg = $("#priceg").is(":checked") ? 100 : 0;
var hhh = $("#priceh").is(":checked") ? 100 : 0;
var iii = $("#pricei").is(":checked") ? 100 : 0;
total.val(+1500 + +aaa + +bbb + +ccc + +ddd + +eee + +fff + +ggg + +hhh + +iii);
var GBP = parseInt(total.val());
var EUR = fx.convert(GBP, {to: "EUR"});
var USD = fx.convert(GBP, {to: "USD"});
var CAD = fx.convert(GBP, {to: "CAD"});
var SGD = fx.convert(GBP, {to: "SGD"});
var AUD = fx.convert(GBP, {to: "AUD"});
var ZAR = fx.convert(GBP, {to: "ZAR"});
EUR = (Math.round(EUR / 100) * 100);
USD = (Math.round(USD / 100) * 100);
CAD = (Math.round(CAD / 100) * 100);
AUD = (Math.round(AUD / 100) * 100);
SGD = (Math.round(SGD / 100) * 100);
ZAR = (Math.round(ZAR / 100) * 100);
GBP = accounting.formatMoney(GBP, "GBP £ ", 0, ",", ".");
EUR = accounting.formatMoney(EUR, "EUR € ", 0, ",", ".");
USD = accounting.formatMoney(USD, "USD $ ", 0, ",", ".");
CAD = accounting.formatMoney(CAD, "CAD $ ", 0, ",", ".");
AUD = accounting.formatMoney(AUD, "AUD $ ", 0, ",", ".");
SGD = accounting.formatMoney(SGD, "SGD $ ", 0, ",", ".");
ZAR = accounting.formatMoney(ZAR, "ZAR R ", 0, ",", ".");
$("#total").val(GBP);
$("#totaleur").val(EUR);
$("#totalusd").val(USD);
$("#totalcad").val(CAD);
$("#totalaud").val(AUD);
$("#totalsgd").val(SGD);
$("#totalzar").val(ZAR);
}
});
// second slider
$("#sliderb").slider({
value: "0",
min: 0,
max: 3,
step: 1,
slide: function(event, ui) {
$("#priceb").val(d[ui.value]);
$("#amountb").val(b[ui.value]);
var aaa = $("#price").val();
var bbb = $("#priceb").val();
var ccc = $("#pricec").val();
var ddd = $("#priced").val();
var eee = $("#pricee").val();
var fff = $("#pricef").val();
var ggg = $("#priceg").is(":checked") ? 100 : 0;
var hhh = $("#priceh").is(":checked") ? 100 : 0;
var iii = $("#pricei").is(":checked") ? 100 : 0;
total.val(+1500 + +aaa + +bbb + +ccc + +ddd + +eee + +fff + +ggg + +hhh + +iii);
var GBP = parseInt(total.val());
var EUR = fx.convert(GBP, {to: "EUR"});
var USD = fx.convert(GBP, {to: "USD"});
var CAD = fx.convert(GBP, {to: "CAD"});
var SGD = fx.convert(GBP, {to: "SGD"});
var AUD = fx.convert(GBP, {to: "AUD"});
var ZAR = fx.convert(GBP, {to: "ZAR"});
EUR = (Math.round(EUR / 100) * 100);
USD = (Math.round(USD / 100) * 100);
CAD = (Math.round(CAD / 100) * 100);
AUD = (Math.round(AUD / 100) * 100);
SGD = (Math.round(SGD / 100) * 100);
ZAR = (Math.round(ZAR / 100) * 100);
GBP = accounting.formatMoney(GBP, "GBP £ ", 0, ",", ".");
EUR = accounting.formatMoney(EUR, "EUR € ", 0, ",", ".");
USD = accounting.formatMoney(USD, "USD $ ", 0, ",", ".");
CAD = accounting.formatMoney(CAD, "CAD $ ", 0, ",", ".");
AUD = accounting.formatMoney(AUD, "AUD $ ", 0, ",", ".");
SGD = accounting.formatMoney(SGD, "SGD $ ", 0, ",", ".");
ZAR = accounting.formatMoney(ZAR, "ZAR R ", 0, ",", ".");
$("#total").val(GBP);
$("#totaleur").val(EUR);
$("#totalusd").val(USD);
$("#totalcad").val(CAD);
$("#totalaud").val(AUD);
$("#totalsgd").val(SGD);
$("#totalzar").val(ZAR);
}
});
slidera
这对, sliderb
, sliderc
, sliderd
, slidere
,重复 6 次sliderf
。唯一的区别在于函数的前两行:
$("#price").val(t[ui.value]);
$("#amount").val(p[ui.value]);
在这里,t
每次p
都是不同的(随机)字母。