我无法理解如何简化以下代码。以这种方式复制代码似乎是多余的。它用于http://boboco.techef.net/express.php
$('#squareDrop').change(function(){
var squarePriceCurrent = $('#squarePrice').text();
var squarePrice = $('#squareDrop option:selected').attr('title');
$('#squarePrice').increment(squarePriceCurrent, squarePrice);
$('#squareTotal').val(squarePrice + '.99');
});
$('#portraitDrop').change(function(){
var portraitPriceCurrent = $('#portraitPrice').text();
var portraitPrice = $('#portraitDrop option:selected').attr('title');
$('#portraitPrice').increment(portraitPriceCurrent, portraitPrice);
$('#portraitTotal').val(portraitPrice + '.99');
});
$('#landscapeDrop').change(function(){
var landscapePriceCurrent = $('#landscapePrice').text();
var landscapePrice = $('#landscapeDrop option:selected').attr('title');
$('#landscapePrice').increment(landscapePriceCurrent, landscapePrice);
$('#landscapeTotal').val(landscapePrice + '.99');
});
答案 0 :(得分:1)
尝试
$('#squareDrop, #portraitDrop, #landscapeDrop').change(function(){
var priceId = this.id.replace('Drop', 'Price'), totalId = this.id.replace('Drop', 'Total');
var priceCurrent = $('#' + priceId).text();
var price = $('option:selected', this).attr('title');
$('#' + priceId).increment(priceCurrent, price);
$('#' + totalId).val(price + '.99');
});