2

这是我的第一篇文章,我有两个问题:

  1. 当我从上到下选择(回答)所有内容时,计算工作正常。如果我更改“日期”,它会重新计算,但是当我之后更改“box2”时,它不会重新计算。有解决办法吗?

  2. 因为这是我的第一个脚本,所以我想知道它是否有用?

    $(document).ready(function() {
    
    $("#box").chained("#box2").change(function() {
    
    var selector = function (dateStr) {
    var d1 = $('#from').datepicker('getDate');
    var d2 = $('#to').datepicker('getDate');
    var diff = 0;
    
        if (d1 && d2) {
            diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
        }
    
        $('#days').val(diff);
    
    var ttl = $('#totaal');
    var discon = $('#discount-container');
    var disc = $('#discount');
    var tax = $('#tax');
    var price = $('#box option:selected').attr('data-price');
    var total = diff * price;
    var tax7 = total*0.07;
    var disc75 = total*0.075;
    var disc15 = total*0.15;
    
    
    if (diff >7 && diff <15) {
        discon.show('slow');
        disc.val("- " + disc75.toFixed(2));
        tax.val("+ "+ tax7.toFixed(2));
        ttl.val("Naf. "+  (total+tax7).toFixed(2));
    } 
    else if (diff >14 ) {
        discon.show('slow');
        disc.val("- " + disc15.toFixed(2));
        tax.val("+ "+ tax7.toFixed(2));
        ttl.val("Naf. "+  (total+tax7).toFixed(2));
    } 
    else {
        discon.hide('slow');
        tax.val("Naf. "+ tax7.toFixed(2));
        ttl.val("Naf. "+  (total+tax7).toFixed(2));
    }
     }  
    
        $('#from').datepicker({
        dateFormat: 'D dd-mm-yy', 
        minDate: '+1D',
        maxDate: '+2Y',
        changeMonth: true,
        numberOfMonths: 1,
        onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
        }
    });
    $('#to').datepicker({
        dateFormat: 'D dd-mm-yy', 
        minDate: '+1D',
        maxDate: '+2Y',
        changeMonth: true,
        numberOfMonths: 1,
        onClose: function( selectedDate ) {
        $('#from').datepicker( "option", "maxDate", selectedDate );
        }
    });
    
    $('#from,#to').change(selector)
    
    
    });
    
    });
    
4

1 回答 1

0

1.

$("#box").chained("#box2").change(function() {
$("#box, #box2").change(function() {

2. 您的代码看起来不错,但是因为您没有很多元素实例,我建议您放弃将每个选择器定义为变量。

于 2012-11-01T16:52:33.087 回答