How to reset a jQuery UI slider to null with multiple jQuery libraries

时间:2016-04-04 16:56:07

标签: javascript jquery html5 slider jquery-ui-slider

I am having trouble resetting jQuery UI sliders.

I get this error: MyProject.OrderInfo.Search.js:187 Uncaught TypeError: $(...).slider is not a function

I think our problem has something to do with having multiple jQuery/Javascript libraries, stepping over each other, but I am uncertain. Everything my team has been trying has not worked. We simply need to reset our sliders to have NULL values. We need to use NULL, so that we know the user has not touched the slider. That way, we can exclude it from our API call parameters.

How can I reset the slider values to NULL?

Here is what the code looks like:

[OrderInfo.cshtml] (script area, top section):

<link rel="stylesheet" href="~/Content/order.css" />
<link rel="stylesheet" href="~/Content/bootstrap.css" />
<link rel="stylesheet" href="~/Content/backgrid-paginator.css" />
<link rel="stylesheet" href="~/Content/backgrid-filter.css" />
<link rel="stylesheet" href="~/Content/backgrid.css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

<script src="~/Scripts/underscore.js"></script>
<script src="~/Scripts/jquery-2.2.0.js"></script>
<script src="~/Scripts/jquery-2.2.0.min.js"></script>
<script src="~/Scripts/backbone.js"></script>
<script src="~/Scripts/backbone.paginator.js"></script>
<script src="~/Scripts/backgrid.js"></script>
<script src="~/Scripts/backgrid-paginator.js"></script>
<script src="~/Scripts/backgrid-filter.js"></script>
<script src="~/Scripts/backgrid-select-filter.js"></script>
<script src="~/Scripts/MyProject.OrderInfo.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="~/Scripts/jquery.ui.touch-punch.js"></script>

[OrderInfo.cshtml] (body):

<div class="col-sm-7" id="divslider">
        <div class="row filterHeader">
            <div class="col-md-3 paddingZero">
                <b>Tuition and Fees</b>
            </div>
            <div class="col-md-2 paddingZero">
                <div class="input-group">
                    Min:
                    <span class="input-group-addon" id="MinOrderPrice"> </span>
                </div>
            </div>
            <div class="col-md-5">
                <div id="SliderOrderPrice"></div>
            </div>
            <div class="col-md-2 paddingZero">
                <div class="input-group">
                    Max:
                    <span class="input-group-addon" id="MaxOrderPrice"> </span>
                </div>
            </div>
        </div>

        <!-- MORE HTML BODY CODE HERE, UI/search elements, DIVS, ETC ETC -->

        <div class="row filterHeader">
            <div class="col-md-3 paddingZero">                       
            </div>
            <div class="col-md-2 paddingZero">                        
            </div>
            <div class="col-md-4">                        
            </div>
            <div class="col-md-3 paddingZero">   
                <button class="btn btn-info" id="btnSearch" type="button">Search</button>
                <button class="btn btn-primary" id="btnReset" type="button" style="min-width:71px">Reset</button>                     
            </div>
        </div>
</div>

<!-- END OF [OrderInfo.cshtml] BODY, ** NOTICE THE BOTTOM HAS A JS SCRIPT, EEEK!! ** -->
<script src="~/Scripts/MyProject.OrderInfo.Search.js"></script>

[MyProject.OrderInfo.Search.js] (a .JS file for searching Orders REST API):

//Declare slider variables as null and assign only in change function, to distinguish 'dirty' slider from a 'clean' slider
var minorderprice;
var maxorderprice;

//Tution and Fees slider
$("#SliderOrderPrice").slider({
    range: true,
    min: 0,
    max: 50000,
    values: [0, 50000],
    step: 500,
    slide: function (event, ui) {
        $("#MinOrderPrice").html("$" + ui.values[0].toLocaleString());
        if (ui.values[1] == 50000) {
            $("#MaxOrderPrice").html("> $50,000")
        }
        else ($("#MaxOrderPrice").html("$" + ui.values[1].toLocaleString()))
    },
    change: function (event, ui) {
        minorderprice = ui.values[0];
        maxorderprice = ui.values[1];
    }
});
$("#MinOrderPrice").html("$ 0");
$("#MaxOrderPrice").html("> $50,000");
$("#SliderOrderPrice").draggable(); //this is a hack to make the slider responsive on mobile devices (see touch-punch.js)

// Get the values of search checkboxes and sliders, after search button click 
$("#btnSearch").click(function () {

    //do a bunch of nifty things to produce a dynamic URL string, for REST API calls here. This works fine and is left out on purpose.

});

$("#btnReset").click(function () {
    $('input:checkbox').removeAttr('checked');     //clear all checkboxes
    $('#orderTerritory').prop('selectedIndex',0);  //reset State/Territory dropdown to 'All States'
    RenderJSGrid();                                //re-draw grid

   //how to reset slider values here?
   $("#SliderOrderPrice").slider('values', 0, 50000);    //this throws the error like the slider doesnt exist?
   $("#SliderOrderPrice").slider('values', null, null); //would this work?
});

Notice there are references to:

<script src="~/Scripts/jquery-2.2.0.js"></script>
<script src="~/Scripts/jquery-2.2.0.min.js"></script>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

We have to do this because our BackgridJS implementation uses jQuery 2.2. The sliders want to use 'jquery-ui.js', which I believe depends upon 'jquery-1.10.2.js'.

When the user clicks the Reset button, the error occurs and javascript acts like the slider isn't loaded. But it is, I can see it working in the UI. Could it be that the Reset button event needs to move somewhere else?

Any ideas? Help?

UPDATE #1: I tried @Noctane's suggestion, but I still get the same error, no matter where I put the Reset function. I can put it inline, I can put it in other scripts but it just never works unfortunately.

Here is what I tried:

<link rel="stylesheet" href="~/Content/order.css" />
<link rel="stylesheet" href="~/Content/bootstrap.css" />
<link rel="stylesheet" href="~/Content/backgrid-paginator.css" />
<link rel="stylesheet" href="~/Content/backgrid-filter.css" />
<link rel="stylesheet" href="~/Content/backgrid.css" />
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="~/Scripts/underscore.js"></script>
<script src="~/Scripts/backbone.js"></script>
<script src="~/Scripts/backbone.paginator.js"></script>
<script src="~/Scripts/backgrid.js"></script>
<script src="~/Scripts/backgrid-paginator.js"></script>
<script src="~/Scripts/backgrid-filter.js"></script>
<script src="~/Scripts/backgrid-select-filter.js"></script>
<script src="~/Scripts/MyProject.OrderInfo.js"></script>
<script src="~/Scripts/jquery.ui.touch-punch.js"></script>

1 个答案:

答案 0 :(得分:1)

I have prepared a demo using jsFiddle, that uses jquery UI while utilizing both jquery v2.2.0 and jquery version 1.10.2, please have a look, you will see that as you move the slider it will set its changed value to the console, when you click reset it will set the slider to null.

See DEMO

You can achieve this with the following code:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" />
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<style>
#resetButton {
  margin-top: 15px;
}

</style>



<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

<script type="text/javascript">
$( "#sliderTest" ).slider({
  change: function( event, ui ) {
    var s = ui.value;
    console.log(s);
  }
});

$("#resetButton").button({
    icons: {
        primary: "ui-icon-refresh"
      }
});

$("#resetButton").click("click", function(event){
        var s = $("#sliderTest").slider("value", null);
    console.log(s);
});
</script>

<div id="sliderTest"></div>
<button id="resetButton">Reset</button>

You may need to re-arrange your includes in your application to get it to work. If you re-arrange the ones in the fiddle you will see the error you are getting, if you then switch them back to the way I have them arranged you will see it work.