2013-10-13 55 views
1

http://jsfiddle.net/Slashus/wduac/46/jQuery的滑块自动刷新变量

我试图删除计算按钮,并有所有的变量自动更新屏幕作为用户使用滑块(如“英镑”一样)。但我所做的一切都失败了。

function numberWithCommas(x) { 
return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); } 

$(function() { 
    $("#slider-range-max").slider({ 
     range: "max", 
     min: 50, 
     max: 20000, 
     value: 50, 
     slide: function (event, ui) { 
      $("#amount").val(ui.value); 
     } 
    }); 

$("#amount").val($("#slider-range-max").slider("value")); 
$("#radio").buttonset(); 

$("button").button().click(function (event) { 
    event.preventDefault(); 
    var value1 = parseInt($("#amount").val()); //get value of slider 
    var result = 0; 
    var radioval = $('input[name=radio]:checked', '#myForm').val(); 
    var tons = (value1/2000).toFixed(1); 

    //By the pound bulk prices 
    if (radioval == "handpicked" && value1 < 2000) result = value1 * 7.50; 
    else if (radioval == "minerun" && value1 < 2000) result = value1 * 4.50; 
    else if (radioval == "hematitemix" && value1 < 2000) result = value1 * 3.50; 

    //By the ton bulk prices 
    else if (radioval == "handpicked" && value1 > 2000) result = value1 * 5.99; 
    else if (radioval == "minerun" && value1 > 2000) result = value1 * 3.99; 
    else if (radioval == "hematitemix" && value1 > 2000) result = value1 * 2.99; 

    //Show Results 
    var perpoundamt = (result/value1).toFixed(2); 
    var finalcostraw = Math.round(result); 
    var finalcost = numberWithCommas(finalcostraw); 

    $("#resultlabel").text("Result: " + "$" + finalcost); //show result 
    $("#perpoundlabel").text("Per Pound " + "$" + perpoundamt); //show result 
    $("#tonslabel").text("Number of tons " + tons); //show result 
}); 

});

+0

@Pachonk这是有道理的,只是顶部有一行未格式化的代码 – Dan

+0

谢谢你。 – Dan

回答

0

在这里你去:http://jsfiddle.net/nadCx/

你需要这样的滑块内的变化选项...

$("#slider-range-max").slider({ 
    range: "max", 
    min: 50, 
    max: 20000, 
    value: 50, 
    slide: function (event, ui) { 
     $("#amount").val(ui.value); 
    }, 
    change: function(event, ui) { 
     alert("Changing, this is where button code goes"); 
    } 
}); 

全码

function numberWithCommas(x) { 
    return x.toString().replace(/\B(?=(?:\d{3})+(?!\d))/g, ","); 
} 

$(function() { 

    $("#slider-range-max").slider({ 
     range: "max", 
     min: 50, 
     max: 20000, 
     value: 50, 
     slide: function (event, ui) { 
      $("#amount").val(ui.value); 
     }, 
     change: function(event, ui) { 

      //This happends when you change the slider value 
      event.preventDefault(); 
      var value1 = parseInt($("#amount").val()); //get value of slider 
      var result = 0; 
      var radioval = $('input[name=radio]:checked', '#myForm').val(); 
      var tons = (value1/2000).toFixed(1); 

      //By the pound bulk prices 
      if (radioval == "handpicked" && value1 < 2000) result = value1 * 7.50; 
      else if (radioval == "minerun" && value1 < 2000) result = value1 * 4.50; 
      else if (radioval == "hematitemix" && value1 < 2000) result = value1 * 3.50; 

      //By the ton bulk prices 
      else if (radioval == "handpicked" && value1 > 2000) result = value1 * 5.99; 
      else if (radioval == "minerun" && value1 > 2000) result = value1 * 3.99; 
      else if (radioval == "hematitemix" && value1 > 2000) result = value1 * 2.99; 

      //Show Results 
      var perpoundamt = (result/value1).toFixed(2); 
      var finalcostraw = Math.round(result); 
      var finalcost = numberWithCommas(finalcostraw); 

      $("#resultlabel").text("Result: " + "$" + finalcost); //show result 
      $("#perpoundlabel").text("Per Pound " + "$" + perpoundamt); //show result 
      $("#tonslabel").text("Number of tons " + tons); //show result 

     } 
    }); 

    $("#amount").val($("#slider-range-max").slider("value")); 
    $("#radio").buttonset(); 
}); 
+0

真棒谢谢你!有没有一种方法可以在滑动时更新它,而不仅仅是在放开滑块之后?然后让变量再次更新,如果用户在3个选项之间切换,同时将滑块保留在原来的位置?我来自视觉基本世界,我迷失在JavaScript语义:( – Dan