开发者

jQuery range slider - get range values on form submission?

I have a jQuery UI range slider set up. When the slider controls are moved my code inserts the lower range value into a hidden input, and also the higher range value into a hidden input. These values constantly change as the slider is moving, but there is a problem. If the lower value stops on £60,000 for example, the value inserted into the hidden div will be £40,000. It is always 1 position out of sync. This is my first problem I need to fix. The values are set as #minPrice and #maxPrice in my code.

The second problem occurs on form submission. I need to get the values of the hidden inputs and (on the page load after the form submission) pass them back the to the text input that shows the range for the sliders e.g. 40,000 to 60,000. This input has the ID of #amount in the code.

Here is my code:

// Set increments for sale slider prices
    var increments = ['0', '20,000', '40,000', '60,000', '80,000', '100,000', '120,000', '140,000', '160,000', '180,000', '200,000', '220,000', '240,000', '260,000', '280,000', '300,000', '320,000', '340,000', '360,000', '380,000', '400,000', '420,000', '440,000', '460,000', '480,000', '500,000', '520,000', '540,000', '560,000', '580,000', '600,000', '620,000', '640,000', '660,000', '680,000', '700,000', '720,000', '740,000', '760,000', '780,000', '800,000', '820,000', '840,000', '860,000', '880,000', '900,000', '920,000', '940,000', '960,000', '980,000', '1,000,000', '1,100,000', '1,200,000', '1,300,000', '1,400,000', '1,500,000', '1,600,000', '1,700,000', '1,800,000', '2,000,000']

$('#sliderPriceSale').slider({
    range: true,
    min: 0,
    step: 1,
    max: 59,
    values: [4, 15],
    slide: function (event, ui) {
        $('#amount').val(increments[ui.values[0]] + "开发者_运维百科 to " + increments[ui.values[1]]);

        var minPrice = $("#sliderPriceSale").slider("values", 0);
        $('#minPrice').val(increments[minPrice]);
        var maxPrice = $("#sliderPriceSale").slider("values", 1);
        $('#maxPrice').val(increments[maxPrice]);
    }
});
$('#amount').val(increments[$('#sliderPriceSale').slider("values", 0)] + " to " + increments[$('#sliderPriceSale').slider("values", 1)]);   

Any help will be much appreciated.


To the first problem, you were using different means to set the #minPrice/#maxPrice and #amount inputs. I cleaned up the code a making it more readable and storing DOM objects and other important data into variables. This increases performance and makes the code much easier to debug. This refactored code always reports the correct values from the slider.

<script type="text/javascript">
$(document).ready(function() { 
    // Set increments for sale slider prices
    var increments = ['0', '20,000', '40,000', '60,000', '80,000', '100,000', '120,000', '140,000', '160,000', '180,000', '200,000', '220,000', '240,000', '260,000', '280,000', '300,000', '320,000', '340,000', '360,000', '380,000', '400,000', '420,000', '440,000', '460,000', '480,000', '500,000', '520,000', '540,000', '560,000', '580,000', '600,000', '620,000', '640,000', '660,000', '680,000', '700,000', '720,000', '740,000', '760,000', '780,000', '800,000', '820,000', '840,000', '860,000', '880,000', '900,000', '920,000', '940,000', '960,000', '980,000', '1,000,000', '1,100,000', '1,200,000', '1,300,000', '1,400,000', '1,500,000', '1,600,000', '1,700,000', '1,800,000', '2,000,000'];
    var amount = $('#amount');
    var minPrice = $('#minPrice');
    var maxPrice = $('#maxPrice');
    var slider = $('#sliderPriceSale');
    var initValuesMinIndex = $.inArray(minPrice.val(), increments); // Get index of value in #minPrice
    var initValuesMaxIndex = $.inArray(maxPrice.val(), increments); // Get index of value in #maxPrice
    var minPriceString = '';
    var maxPriceString = '';

    // Setup slider. Initialize #amount on creation.
    slider.slider({
        range: true,
        min: 0,
        max: 59,
        step: 1,
        values: [initValuesMinIndex, initValuesMaxIndex],
        create: function() {
            amount.val(increments[initValuesMinIndex] + " to " + increments[initValuesMaxIndex]);
        },
        slide: function (event, ui) {
            // Get max/min price strings. If max price is == largest value append "+" sign.
            minPriceString = increments[ui.values[0]];
            maxPriceString = (increments[ui.values[1]] == increments[increments.length-1]) ? increments[ui.values[1]] + "+" : increments[ui.values[1]];

            amount.val(minPriceString + " to " + maxPriceString);
            minPrice.val( increments[ui.values[0]] );
            maxPrice.val( increments[ui.values[1]] );                 
        }
    });
});
</script>

As for the second problem, it's hard to say exactly how you should accomplish that without seeing the rest of your code but in general all you have to do is make sure #maxPrice and #minPrice hidden variables are set to the correct values when the page loads and the slider will automatically display the correct information when it is instantiated because it checks for those values when it is constructed. For example, if you are using PHP and submitting the form via POST then you might use the following code for those hidden vars:

<!-- Output minPrice & maxPrice values. Default to 80,000 and 300,000 respectively -->
<input type="hidden" id="minPrice" name="minPrice" value="<?php echo (isset($_POST['minPrice'])) ? $_POST['minPrice'] : '80,000'; ?>" />
<input type="hidden" id="maxPrice" name="maxPrice" value="<?php echo (isset($_POST['maxPrice'])) ? $_POST['maxPrice'] : '300,000'; ?>" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜