-1

我不知道如何简化以下代码。以这种方式复制代码似乎是多余的。它用于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');
});
4

1 回答 1

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');
});
于 2013-07-16T07:24:53.857 回答