将值从2 jQuery范围滑块传递到PHP

时间:2013-11-19 08:50:15

标签: jquery rangeslider

我想在mysql / php中使用范围滑块进行查询以获取列表。 但我不明白如何将变量从jQuery传递到php文件。 我读过this page和其他人,但仍然没有得到结果。 这是我想传递的jQuery代码,我该怎么做才能传递变量?

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery UI Slider - Range slider</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
        $(function() {
            $( "#slider-range1" ).slider({
                range: true,
                min: 0,
                max: 500,
                values: [ 75, 300 ],
                slide: function( event, ui ) {
                    $( "#amount1" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
                }
            });
            $( "#amount1" ).val( "$" + $( "#slider-range1" ).slider( "values", 0 ) +
                " - $" + $( "#slider-range1" ).slider( "values", 1 ) );

            $( "#slider-range2" ).slider({
                range: true,
                min: 0,
                max: 500,
                values: [ 75, 300 ],
                slide: function( event, ui ) {
                    $( "#amount2" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
                }
            });
            $( "#amount2" ).val( "$" + $( "#slider-range2" ).slider( "values", 0 ) +
                " - $" + $( "#slider-range2" ).slider( "values", 1 ) );

            $.ajax("file.php", {variable1:/*variable value*/, variable2:/*variable value*/, variable3:/*variable value*/, variable4:/*variable value*/}, /* insert ajax result*/);
        });
    </script>
</head>
<body>
    <p>
        <label for="amount1">Price range:</label>
        <input type="text" id="amount1" style="border: 0; color: #f6931f; font-weight: bold;" />
    </p>
    <div id="slider-range1"></div>
    <p>
        <label for="amount2">Price range:</label>
        <input type="text" id="amount2" style="border: 0; color: #f6931f; font-weight: bold;" />
    </p>
    <div id="slider-range2"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

如果您想在每次更改时提交值,您可以尝试我的代码,但如果您这样做,那么您有一个坏主意..更好的是您有一个按钮告诉您的服务器客户端已经完成了选择价格。

我只是更新你的代码我希望它有所帮助。

    <script>
        $(function() {
            var sliderChangeCallback = function(){
               var amount1 = $( "#amount1" ).val(), amount2 = $( "#amount2" ).val()

               $.ajax({
                   url : "your url",
                   data : {
                       "amount1" : amount1,
                       "amount2" : amount1
                   },
                   error : function(){
                       "call back error"
                   }
               })
            };

            $( "#slider-range1" ).slider({
                range: true,
                min: 0,
                max: 500,
                values: [ 75, 300 ],
                slide: function( event, ui ) {
                    $( "#amount1" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
                    sliderChangeCallback();
                }
            });
            $( "#amount1" ).val( "$" + $( "#slider-range1" ).slider( "values", 0 ) +
                " - $" + $( "#slider-range1" ).slider( "values", 1 ) );

            $( "#slider-range2" ).slider({
                range: true,
                min: 0,
                max: 500,
                values: [ 75, 300 ],
                slide: function( event, ui ) {
                    $( "#amount2" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
                    sliderChangeCallback();
                }
            });
            $( "#amount2" ).val( "$" + $( "#slider-range2" ).slider( "values", 0 ) +
                " - $" + $( "#slider-range2" ).slider( "values", 1 ) );

        });
    </script>