如何计算滑块的总值

时间:2014-07-30 17:09:09

标签: javascript jquery html

我的页面上有两组滑块。一组有四个滑块,另一组有六个滑块。当您在我页面左上角的4cos和6cos之间切换时,可以看到这两个集合。我想独立计算每组滑块的总值,并在单击页面上的“验证”按钮时将总计显示为警报。我无法正确解释。如果您有任何疑问,请询问我。这是jsfiddle http://jsfiddle.net/ApnUm/5

这是我的代码。

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="jquery.nouislider.css"/>
<!-- <link rel="stylesheet" href="att.framework.css"/> -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="jquery.nouislider.min.js"></script>
</head>
<body>
<div class="cos_buttonsDiv">
<div class="cos4Button btn_bgColor">4 CoS</div>
<div class="cos6Button">6 CoS</div>

<div class="ConfigureCos4Header">
<div>CoS1 is dedicated to Real Time traffic.</div>
<div>Please select a value from 0% to 100%</div>
</div>
<div class="ConfigureCos6Header">
<div>Real Time Traffic is dedicated to CoS1.</div>
<div>The available values diff based on the CoS Package</div>
</div>

</div>

<!-- 4 sliders Div-->

<div class="Configure4Div">
<div class="ConfigureCos4">
<div>CoS 1 Real Time</div>
    <div class="example" id="cos4_darkBlue">
    <div class="slider-connect" id="cos4_slider1"> 
    </div>
    <div id="cos4_field1"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>  


<div class="ConfigureCos_4">
    <div class="slide_Div">

    <div class="example" id="cos4_green">
    <div class="slider-connect" id="cos4_slider2">
    </div>
    <div id="cos4_field2"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>


    <div class="slide_Div">
    <div class="example" id="cos4_pink">
    <div class="slider-connect" id="cos4_slider3">
    </div>
      <div id="cos4_field3"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>

     <div class="slide_Div">
    <div class="example" id="cos4_orange">
    <div class="slider-connect" id="cos4_slider4"></div>
    <div id="cos4_field4"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
    </div>
</div>
</div>
</div>
<!-- 4 sliders Div close-->

<!-- 6 sliders Div-->
<div class="Configure6Div">
<div class="ConfigureCos6">
<div>CoS 1 Real Time</div>
    <div class="example" id="Cos6_Darkblue">
    <div class="slider-connect" id="cos6_slider1"></div>
    <div id="cos6_field1"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>

    <div class="ConfigureCos_6">
     <div class="slide_Div">
    <div class="example"id="Cos6_lightblue" >
    <div class="slider-connect" id="cos6_slider2"></div>
    <div id="cos6_field2"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>

     <div class="slide_Div">
    <div class="example" id="Cos6_green">
    <div class="slider-connect" id="cos6_slider3"></div>
    <div id="cos6_field3"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>

     <div class="slide_Div">
    <div class="example" id="Cos6_pink">
    <div class="slider-connect" id="cos6_slider4"></div>
      <div id="cos6_field4"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>

     <div class="slide_Div">
    <div class="example" id="Cos6_gold">
    <div class="slider-connect" id="cos6_slider5"></div>
    <div id="cos6_field5"></div>
    </div>

    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>

     <div class="slide_Div">
    <div class="example" id="Cos6_Darkpink">
    <div class="slider-connect" id="cos6_slider6"></div>
    <div id="cos6_field6"></div>
    </div>
    <div class="plusMinusDiv">
    <div class="minusButton"><span style="line-height:25px;font-size:33px;padding-left:11px;">-</span></div>
    <div class="plusButton"><span class="icon-plus" style="line-height:28px;font-size:16px;padding-left:7px;">+</span></div>
</div>
</div>
</div>
</div>

<div class="fixed-size-square">
    <span class="cos_validate_button" id="mybtn"><button  id="validate_btn" class="btn btn-val btn-small">Validate</button></span>
</div>
<hr style="border-bottom:0px solid #D3D3D2; width:1100px; margin-top:49px;">
<!--<div class="callout bottom">The Selection is not a valid profile for the CoS Package. Please select another profile.</div>-->
<table width="100%">
<tr>
 <td><div class="cos_buttons">      
            <span class="cos_review_button"><button id="review_btn" class="btn btn-primary btn-small">Review</button></span>&nbsp;
            <span class="cos_cancel_button"><a class="cancel-redirectChangeIP" href="javascript:void(0)"><button class="btn btn-small cancelButtonChangeIP">Cancel</button></a></span>
        </div></td>
 </tr>
</table>
<!-- 6 sliders Div close-->

<!-- <script src="att.framework.js"></script> -->
<script>

</script>
<script src="js/script.js"></script>
</body>
</html>

CSS


/* Functional styling;
 * These styles are required for noUiSlider to function.
 * You don't need to change these rules to apply your design.
 */
.noUi-target,
.noUi-target * {
-webkit-touch-callout: none;
-webkit-user-select: none;
-ms-touch-action: none;
-ms-user-select: none;
-moz-user-select: none;
-moz-box-sizing: border-box;
    box-sizing: border-box;
}
.noUi-base {
    width: 100%;
    height: 100%;
    position: relative;
}
.noUi-origin {
    position: absolute;
    right: 0;
    top: 0;
    left: 0;
    bottom: 0;
}
.noUi-handle {
    position: relative;
    z-index: 1;
}
.noUi-stacking .noUi-handle {
/* This class is applied to the lower origin when
   its values is > 50%. */
    z-index: 10;
}
.noUi-stacking + .noUi-origin {
/* Fix stacking order in IE7, which incorrectly
   creates a new context for the origins. */
    *z-index: -1;
}
.noUi-state-tap .noUi-origin {
-webkit-transition: left 0.3s, top 0.3s;
    transition: left 0.3s, top 0.3s;
}
.noUi-state-drag * {
    cursor: inherit !important;
}

/* Slider size and handle placement;
 */
.noUi-horizontal {
    height: 18px;
}
.noUi-horizontal .noUi-handle {
    width: 34px;
    height: 28px;
    left: -17px;
    top: -6px;
}
.noUi-horizontal.noUi-extended {
    padding: 0 15px;
}
.noUi-horizontal.noUi-extended .noUi-origin  {
    right: -15px;
}
.noUi-vertical {
    width: 18px;
}
.noUi-vertical .noUi-handle {
    width: 28px;
    height: 34px;
    left: -6px;
    top: -17px;
}
.noUi-vertical.noUi-extended {
    padding: 15px 0;
}
.noUi-vertical.noUi-extended .noUi-origin  {
    bottom: -15px;
}

/* Styling;
 */
.noUi-background {
    background: #FAFAFA;
    box-shadow: inset 0 1px 1px #f0f0f0;
}
.noUi-connect {
    background: #3FB8AF;
    box-shadow: inset 0 0 3px rgba(51,51,51,0.45);
-webkit-transition: background 450ms;
    transition: background 450ms;
}
.noUi-origin {
    border-radius: 2px;
}
.noUi-target {
    border-radius: 4px;
    border: 1px solid #D3D3D3;
    box-shadow: inset 0 1px 1px #F0F0F0, 0 3px 6px -5px #BBB;
}
.noUi-target.noUi-connect {
    box-shadow: inset 0 0 3px rgba(51,51,51,0.45), 0 3px 6px -5px #BBB;
}

/* Handles and cursors;
 */
.noUi-dragable {
    cursor: w-resize;
}
.noUi-vertical .noUi-dragable {
    cursor: n-resize;
}
.noUi-handle {
    border: 1px solid #D9D9D9;
    border-radius: 3px;
    background: #FFF;
    cursor: default;
    box-shadow: inset 0 0 1px #FFF,
                inset 0 1px 7px #EBEBEB,
                0 3px 6px -3px #BBB;
}
.noUi-active {
    box-shadow: inset 0 0 1px #FFF,
                inset 0 1px 7px #DDD,
                0 3px 6px -3px #BBB;
}

/* Handle stripes;
 */
.noUi-handle:before,
.noUi-handle:after {
    content: "";
    display: block;
    position: absolute;
    height: 14px;
    width: 1px;
    background: #E8E7E6;
    left: 14px;
    top: 6px;
}
.noUi-handle:after {
    left: 17px;
}
.noUi-vertical .noUi-handle:before,
.noUi-vertical .noUi-handle:after {
    width: 14px;
    height: 1px;
    left: 6px;
    top: 14px;
}
.noUi-vertical .noUi-handle:after {
    top: 17px;
}

/* Disabled state;
 */
[disabled].noUi-connect,
[disabled] .noUi-connect {
    background: #B8B8B8;
}
[disabled] .noUi-handle {
    cursor: not-allowed;
}
.cos_validate_button{float:right;margin-right:48px;margin-left:48px;padding-top:30px;margin-bottom:20px;}
.cos_cancel_button{float:left;margin-left:400px;margin-top:50px;margin-bottom:100px;}
.cos_review_button{float:right;margin-right:400px;margin-top:50px;margin-bottom:100px;}
.btn{position:relative;display:inline-block;-moz-background-clip:padding-box;-webkit-background-clip:padding-box;background-clip:padding-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;min-width:100px;padding:12px 30px;padding:1.2rem 2.9rem;border:none;border-radius:6px;background-color:#666666;background-image:none;color:#ffffff;vertical-align:middle;text-align:center;text-decoration:none;text-transform:capitalize;text-shadow:none;white-space:nowrap;font-size:18px;line-height:18px;cursor:pointer;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;-webkit-transition:background-color 0.3s ease-out;-moz-transition:background-color 0.3s ease-out;transition:background-color 0.3s ease-out;}
.btn:hover,.btn:focus{background-color:#bbbbbb;color:#ffffff;text-decoration:none;}
.btn.active,.btn:active{background-color:#666666;color:#bbbbbb;-webkit-transition:background-color 0;-moz-transition:background-color 0;transition:background-color 0;}
.btn{border-top-color:#000000 !important;}
.btn-primary{color:#ffffff;background-color:#067ab4;border-color:none;}
.btn-primary:hover,.open .dropdown-toggle.btn-primary{color:#ffffff;background-color:#199ddf;border-color:none;}
.btn-primary:active,.btn-primary.active{color:#ffffff;background-color:#067ab4;background-image:none;}
.btn-primary:focus{color:#ffffff;background-color:#199ddf;}
.btn-primary.btn-dark-bkgd{color:#ffffff;background-color:#023187;border-color:none;}
.btn-primary.btn-dark-bkgd:hover,.open .dropdown-toggle.btn-primary.btn-dark-bkgd{color:#ffffff;background-color:#014b9d;border-color:none;}
.btn-primary.btn-dark-bkgd:active,.btn-primary.btn-dark-bkgd.active{color:#067ab4;background-color:#6aafd2;background-image:none;}
.btn-primary.btn-dark-bkgd:focus{color:#ffffff;background-color:#199ddf;}
.btn-small{padding:8px 20px;font-size:18px;line-height:21.6px;min-width:60px;width:120px;font-family:clearviewatt;height:41px;padding-top:9px;}
.btn-val{color:#ffffff;background-color:#067ab4;border-color:none;}
.btn-val:hover,.open .dropdown-toggle.btn-val{color:#ffffff;background-color:#199ddf;border-color:none;}
.btn-val:active,.btn-val.active{color:#ffffff;background-color:#067ab4;background-image:none;}
.btn-val:focus{color:#ffffff;background-color:#199ddf;}
.btn-val.btn-dark-bkgd{color:#ffffff;background-color:#023187;border-color:none;}
.btn-val.btn-dark-bkgd:hover,.open .dropdown-toggle.btn-val.btn-dark-bkgd{color:#ffffff;background-color:#014b9d;border-color:none;}
.btn-val.btn-dark-bkgd:active,.btn-val.btn-dark-bkgd.active{color:#067ab4;background-color:#6aafd2;background-image:none;}
.btn-val.btn-dark-bkgd:focus{color:#ffffff;background-color:#199ddf;}

.fixed-size-square {
    border: 1px solid grey;
    border-radius: 5px 5px 5px 5px;
    margin: 50px 0px 0px 0px;
    display: table;
    width: 200px;
    height: 600px;
  }
.fixed-size-square span {
    margin-top: 500px;
 }

Javascript/jQuery

$(document).ready(function(){
$('.ConfigureCos6Header').hide();

$('.cos6Button').click(function(){
$('.cos6Button').addClass('btn_bgColor');
$('.cos4Button').removeClass('btn_bgColor');
$('.Configure6Div').show();
$('.Configure4Div').hide();
$('.ConfigureCos6Header').show();
$('.ConfigureCos4Header').hide();
});

$('.cos4Button').click(function(){
$('.cos4Button').addClass('btn_bgColor');
$('.cos6Button').removeClass('btn_bgColor');
$('.ConfigureCos4Header').show();
$('.ConfigureCos6Header').hide();
$('.Configure6Div').hide();
$('.Configure4Div').show();
});


var toolTip = $.Link({
    target: '-tooltip-'
});
/* cos4 Sliders*/
$("#cos4_slider1").noUiSlider({
    start: 40,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos4_field1")
            }),
            toolTip
            ]
    },

});

$("#cos4_slider2").noUiSlider({
    start: 80,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos4_field2")
            }),
            toolTip
            ]
    },

});

$("#cos4_slider3").noUiSlider({
    start: 20,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos4_field3")
            }),
            toolTip
            ]
    },

});


$("#cos4_slider4").noUiSlider({
    start: 60,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos4_field4")
            }),
            toolTip
            ]

    },

});
/* cos4 Sliders close*/


/* cos6 Sliders*/


$("#cos6_slider1").noUiSlider({
    start: 60,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos6_field1")
            }),
            toolTip
            ]
    },

});


$("#cos6_slider2").noUiSlider({
    start: 17,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos6_field2")
            }),
            toolTip
            ]
    },

});

$("#cos6_slider3").noUiSlider({
    start: 15,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos6_field3")
            }),
            toolTip
            ]

    },

});

$("#cos6_slider4").noUiSlider({
    start: 20,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos6_field4")
            }),
            toolTip
            ]
    },

});
$("#cos6_slider5").noUiSlider({
    start: 20,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos6_field5")
            }),
            toolTip
            ]
    },

});


$("#cos6_slider6").noUiSlider({
    start: 28,
    connect: "lower",
    range: {
      'min': 0,
      'max': 100
    },
    serialization: {
        lower: [ $.Link({
                target: $("#cos6_field6")
            }),
            toolTip
            ]
    },

});
/* cos6 Sliders close*/
$('#review_btn').removeClass('btn-primary');                            
        $('.cos_validate_button').click(function(){
            alert(toolTip);

            });

});

1 个答案:

答案 0 :(得分:1)

也许这就是你要找的东西:

$('.cos_validate_button').click(function(){
    var total6 =   parseFloat($("#cos6_slider1").val()) +
        parseFloat($("#cos6_slider2").val()) +
        parseFloat($("#cos6_slider3").val()) +
        parseFloat($("#cos6_slider4").val()) +
        parseFloat($("#cos6_slider5").val()) +
        parseFloat($("#cos6_slider6").val());
    var total4 = parseFloat($("#cos4_slider1").val()) +
        parseFloat($("#cos4_slider2").val()) +
        parseFloat($("#cos4_slider3").val()) +
        parseFloat($("#cos4_slider4").val());
    var total = total6+total4;
    alert("total 6: "+total6+"\ntotal 4: "+total4+"\ntotal: "+total);

    });

工作小提琴here