4

我有一个滑块,它在页面加载时应该在滑块的句柄中产生一个值,并且还有另一个标签值。我不明白为什么在页面加载时我得到 NaN 但是当我移动滑块时,值会出现。

请看一下,这样你就可以亲眼看到Fiddle

代码看起来不错,但值不会出现在页面加载时的句柄中。

$("#slider1").slider({
    max:350,
    min:100,
    step:10,
    value:100,
    animate: 'true',
    slide: function(event, ui) {  
        $("#amount").val(ui.value);
        $(this).find('.ui-slider-handle').html('<div class="sliderControl-label v-labelCurrent">£'+ui.value+'</div>');
                update();
    }    
});

function addDaysToDate(days) {
  var mths = new Array("Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul", "Aug", "Sep",
    "Oct", "Nov", "Dec");

  var d = new Date();
  d.setHours(d.getHours() + (24 * days));

  var currD = d.getDate();
  var currM = d.getMonth();
  var currY = d.getFullYear();

  return mths[currM] + " " + currD + ", " + currY;
}

$("#slider2").slider({
    max:30,
    min:7,
    value:7,
    animate: 'true',    
    slide: function(event, ui) {  

        $("#days").val(ui.value);
            $(this).find('.ui-slider-handle').html('<div class="sliderControl-label v-labelCurrent">'+ui.value+'<span class="unit"> days</span></div>');
        $("#date").text(addDaysToDate(parseInt($("#days").val())));
                update();
    },
    create: function(event, ui) {
      $("#date").text(addDaysToDate(parseInt($("#days").val())));
    }    
});

$("#days").val($("#slider2").slider("value"));

$("#days").change(function(event) {
  var data = $("#days").val();
  if (data.length > 0)
  {
     if (parseInt(data) >= 7 && parseInt(data) <= 31)
     {
         $("#slider2").slider("option", "value", data);
     }
     else
     {
        if (parseInt(data) < 1)
        {
            $("#days").val("7");
            $("#slider2").slider("option", "value", "7");
        }
        if (parseInt(data) > 31)
        {
            $("#days").val("31");
            $("#slider2").slider("option", "value", "31");
        }
     }
  }
  else
  {
    $("#slider2").slider("option", "value", "7");
  }
  $("#date").text(addDaysToDate(parseInt($("#days").val())));
});

update();
4

3 回答 3

9

最初之后;

$amount1 = $("#amount").val();

$amount1是一个空字符串。

当你然后parseInt($amount1)NaN 没有 0得到它然后使整个表达NaN

检测和处理空值,例如$amount1 = parseInt($amount1, 10) || 0;

于 2012-10-29T12:50:02.120 回答
2

您必须$('#amount').val();在调用之前设置为默认值update()

于 2012-10-29T12:49:39.003 回答
0

您应该进行以下更改

HTML

100您应该在<span class="decimal">100</span>like中放置一个默认文本

<div class="loanForm-label">Your loan:</div>
<div class="loanForm-totalToBorrow v-number" id="amount">
    <span class="dollarSign">£</span> <span class="decimal">100</span>
</div>

jQuery

在这里将所有实例更改$("#amount").val$("#amount .decimal").text喜欢

function update() {            
     $interest = 0.15 ;
     $perday = 15 ;
     $amount1 = Number($("#amount .decimal").text());
     if($amount1>0){
         $dayscount =Number($("#days").val());
         $amount2 = $amount1 + $interest * $amount1 + ($dayscount * ($perday/100));
         $apr = ((($amount2-$amount1) / $amount1 ) / (($dayscount/365) * 100)) ;
         $("#amount .decimal").text($amount1);
         $("#amount2").text($amount2);
         $("#amount3").text(parseFloat($amount2-$amount1).toFixed(2));
         $("#amount4").text(parseFloat($apr).toFixed(2));
     }
}


$("#slider1").slider({
    max:350,
    min:100,
    step:10,
    value:100,
    animate: 'true',
    slide: function(event, ui) {  
        $("#amount .decimal").text(ui.value);
        $(this).find('.ui-slider-handle')
               .html('<div class="sliderControl-label v-labelCurrent">£'+ui.value+'</div>');
        update();
    }    
});

function addDaysToDate(days) {
  var mths = new Array("Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul", "Aug", "Sep",
    "Oct", "Nov", "Dec");

  var d = new Date();
  d.setHours(d.getHours() + (24 * days));

  var currD = d.getDate();
  var currM = d.getMonth();
  var currY = d.getFullYear();

  return mths[currM] + " " + currD + ", " + currY;
}

$("#slider2").slider({
    max:30,
    min:7,
    value:7,
    animate: 'true',    
    slide: function(event, ui) {  
        $("#days").val(ui.value);
        $(this).find('.ui-slider-handle')
               .html('<div class="sliderControl-label v-labelCurrent">'+ui.value+'<span class="unit"> days</span></div>');
        $("#date").text(addDaysToDate(parseInt($("#days").val())));
        update();
    },
    create: function(event, ui) {
      $("#date").text(addDaysToDate(parseInt($("#days").val())));
    }    
});

$("#days").val($("#slider2").slider("value"));

$("#days").change(function(event) {
  var data = $("#days").val();
  if (data.length > 0)
  {
     if (parseInt(data) >= 7 && parseInt(data) <= 31)
     {
         $("#slider2").slider("option", "value", data);
     }
     else
     {
        if (parseInt(data) < 1)
        {
            $("#days").val("7");
            $("#slider2").slider("option", "value", "7");
        }
        if (parseInt(data) > 31)
        {
            $("#days").val("31");
            $("#slider2").slider("option", "value", "31");
        }
     }
  }
  else
  {
    $("#slider2").slider("option", "value", "7");
  }
  $("#date").text(addDaysToDate(parseInt($("#days").val())));
});
update();

检查这个演示

于 2012-10-29T13:15:20.607 回答