我有两个ui-slider,并且有一个关于幻灯片回调的计算,但是它返回的是NaN值

$( document ).ready(function() {
  $("#amount").slider({
      range: "min",
      value: 160000,
      min: 10000,
      max: 400000,
      step: 1,
      slide: function(event, ui) {
        var interest = 0.0325;
        var amount = parseInt(ui.value);
        var temp1 = amount * (interest / 12) / (1 - Math.pow(1 + (interest / 12), -(years * 12)));
        $('#currentamount').val(amount);
        $('#monthly').text(Math.round(temp1));
      }
  });
  $("#years").slider({
      range: "min",
      value: 8,
      min: 1,
      max: 12,
      step: 1,
      slide: function(event, ui) {
        var interest = 0.0325;
       var years = parseInt(ui.value);
       var temp1 = amount * (interest / 12) / (1 - Math.pow(1 + (interest / 12), -(years * 12)));
       $('#yearsval').text(years);
       $('#monthly').text(Math.round(temp1));
      }
  });
});


我也做了一个小提琴:http://jsfiddle.net/kristianladd/w0xayf7t/

我之所以将其放在其幻灯片回调中,是因为如果您滑动两者之一,它将进行计算。

最佳答案

只需在调用幻灯片时从其他滑块读取状态即可:

$( document ).ready(function() {
  $("#amount").slider({
      range: "min",
      value: 160000,
      min: 10000,
      max: 400000,
      step: 1,
      slide: function(event, ui) {
        var years = $('#years').slider("option", "value");
        var interest = 0.0325;
        var amount = parseInt(ui.value);
        var temp1 = amount * (interest / 12) / (1 - Math.pow(1 + (interest / 12), -(years * 12)));
        $('#currentamount').val(amount);
        $('#monthly').text(Math.round(temp1));
      }
  });
  $("#years").slider({
      range: "min",
      value: 8,
      min: 1,
      max: 12,
      step: 1,
      slide: function(event, ui) {
       var amount = $('#amount').slider("option", "value");
       var interest = 0.0325;
       var years = parseInt(ui.value);
       var temp1 = amount * (interest / 12) / (1 - Math.pow(1 + (interest / 12), -(years * 12)));
       $('#yearsval').text(years);
       $('#monthly').text(Math.round(temp1));
      }
  });
});

关于javascript - JavaScript返回NaN值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31177511/

10-12 14:25