根据单选按钮和Jquery滑块选择来选择跨度文本

时间:2018-11-30 09:54:24

标签: javascript jquery

我正在尝试汇总产品的选择流程,但是在定义值时遇到了一个小问题。

基本上,我想做的是获得总输出,该总输出基于他们选择的选项乘以行数。我不确定如何执行此操作,因为滑块不会以固定增量递增。

$("#slider").slider(
    {
        value:1,
        min: 0,
        max: 9,
        step: 1,
    }
);

var items =[ '1 <br>line','2 <br>lines','3 <br>lines','4 <br>lines','5 <br>lines','8 <br>lines','10 <br>lines','15 <br>lines','20+ <br>lines'];

var s = $("#slider");

s.slider({
  min:1,
  max:items.length
});

var oneBig = 100 / (items.length - 1);

$.each(items, function(key,value){
  var w = oneBig;
  if(key === 0 || key === items.length-1)
    w = oneBig/2;
    
  $("#legend").append("<label style='width: "+w+"%'>"+value+"</laben>");  
  
});



//sum start ///////////////



$(":radio").on("change", function(){
    var total = 0;
    $(":radio:checked").each(function(){
        total += Number(this.value);
    });
    
   $("#total").text(total);
});
h3 {
  margin-top:30px;
}

#slider label {
    position: absolute;
    width: 20px;
    margin-left: -10px;
    text-align: center;
    margin-top: 20px;
}

#slider {
    width: 100%;
    margin: 2em auto;
}

.lines {
    font-size:10px;
  
}

label {
    display: inline-block;
    text-align:center;
    line-height: 1.5;
    font-weight:bold;
}
label:first-child {
    text-align:left;
}
label:last-child {
    text-align:right;
}

.slide-col #slider a {
    border: 1px solid #fff;
    background: #1b2a3d;
}
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" /><script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>

<input class="qty1" name="phone" type="radio" value="5.99" />option 1
<input class="qty1" name="phone" type="radio" value="9.99" />option 2
<input class="qty1" name="phone" type="radio" value="7.99" />option 3


<div id="slider"></div>
<div id="legend"></div>

<br>

<h3>Your total is: £<span id="total">0.00</span></h3>

2 个答案:

答案 0 :(得分:1)

更新:您可以自定义滑块的值。还增加了计算总价值的功能。

let $slider = $('#slider'),
    $legend = $('#legend'),
    $total = $('#total'),
    total = 0, 
    lines = [1, 2, 3, 4, 5, 8, 10, 15, 20];

lines.forEach( (value, key) => {
  let oneBig = 100 / (lines.length - 1);
  let width = (key === 0 || key === lines.length -1) ? 
              oneBig / 2 : oneBig;
  let text = (key !== lines.length - 1) ?
              value + ' <br>lines' : value + '+ <br>lines';
  let label = $('<label/>')
                .css({ 'width': width + '%'})
                .html(text);
  $legend.append(label);
});

$(":radio").on("change", function(){
  updateTotal(lines[$slider.slider("option", "value")]);
});

$slider.slider({
  value: 0,
  min: 0,
  max: 8,
  step: 1,
  create: () => {
    updateTotal(lines[$slider.slider('value')]);
  },
  slide: (events, ui) => {
    updateTotal(lines[ui.value]);
  }
});

function updateTotal (value) {
  total = value * $("input[name='phone']:checked").val();
  $total.text(total.toFixed(2));
}
h3 {
  margin-top: 10px;
}

#slider label {
    position: absolute;
    width: 20px;
    margin-left: -10px;
    text-align: center;
    margin-top: 20px;
}

#slider {
    width: 100%;
    margin: 1em auto;
}

.lines {
    font-size:10px;
  
}

label {
    display: inline-block;
    text-align:center;
    line-height: 1.5;
    font-weight:bold;
}
label:first-child {
    text-align:left;
}
label:last-child {
    text-align:right;
}

.slide-col #slider a {
    border: 1px solid #fff;
    background: #1b2a3d;
}
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" /><script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>

<input class="qty1" name="phone" type="radio" value="5.99" checked/>option 1
<input class="qty1" name="phone" type="radio" value="9.99" />option 2
<input class="qty1" name="phone" type="radio" value="7.99" />option 3


<div id="slider"></div>
<div id="legend"></div>

<br>

<h3>Your total is: £<span id="total">0.00</span></h3>

答案 1 :(得分:0)

尝试类似这样的方法:http://jsfiddle.net/b64Ug/224/ 如果您注意到,我将'slide'属性附加到您的滑块上,该属性将在每次更改滑块值时调用。您可能需要为边缘情况添加一些检查。

$("#slider").slider(
    {
        value:1,
        min: 0,
        max: 9,
        step: 1,
    }
);
var valueMap = [0, 1, 2, 3, 4, 5, 8, 10, 15, 20];

var items =[ '1 <br>line','2 <br>lines','3 <br>lines','4 <br>lines','5 <br>lines','8 <br>lines','10 <br>lines','15 <br>lines','20+ <br>lines'];

var s = $("#slider");

s.slider({
  min:1,
  max:items.length,
  slide: function( event, ui ) {
        $("#total").text("$" +  (valueMap[ui.value] * $('input[name=phone]:checked').val()));
      }
});

var oneBig = 100 / (items.length - 1);

$.each(items, function(key,value){
  var w = oneBig;
  if(key === 0 || key === items.length-1)
    w = oneBig/2;

  $("#legend").append("<label style='width: "+w+"%'>"+value+"</laben>");  

});



//sum start ///////////////



$(":radio").on("change", function(){
    var total = 0;
    $(":radio:checked").each(function(){
        total = Number(this.value) * valueMap[Number($( "#slider" ).slider( "value" ))];
    });

   $("#total").text(total);
});