为什么我在Jquery UI Slider的页面加载上获得NaN

时间:2012-10-29 12:42:08

标签: jquery jquery-ui

我有一个滑块,在页面加载时应该在滑块的手柄中产生一个值,还有一个标签的值。我无法理解为什么在页面加载时我得到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();

3 个答案:

答案 0 :(得分:9)

最初之后;

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

$amount1是一个空字符串。

然后当您parseInt($amount1)时,NaN 0,然后制作整个表达式NaN

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

答案 1 :(得分:2)

在调用$('#amount').val();

之前,您必须将update()设置为默认值

答案 2 :(得分:0)

您应该在

中进行以下更改

<强> HTML

您应该在100中添加默认文字<span class="decimal">100</span>

<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();

选中 Demo