jquery 范围滑块值到查询字符串

发布于 2024-12-18 15:25:43 字数 1443 浏览 3 评论 0原文

我正在使用 jQuery UI 范围滑块演示代码 (http://jqueryui.com/demos/slider/# range)来创建价格范围滑块。我想将从该范围滑块中选择的值传输到 minValue & 的获取参数。 最大值

我一直在尝试通过让 JavaScript 更新下面表单中的隐藏字段来实现此目的,但这似乎不起作用,因为目前页面重新加载时 URL 查询字符串如下所示: ?minValue= &maxValue=&submit=go 并且没有从范围滑块传输任何值。如何获取范围滑块中的值到查询字符串?

提前致谢。

<script type="text/javascript" language="javascript">
$(function() {
    $( "#slider-range" ).slider({
        range: true,
        min: 1,
        max: 3000,
        values: [ 1, 300 ],
        slide: function( event, ui ) {
            $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ]);
        }
    });
    $( "#amount" ).val( "£" + $( "#slider-range" ).slider( "values", 0 ) + " - £" + $( "#slider-range" ).slider( "values", 1 ) );
    $( "#minValue" ).val( ui.values[ 0 ] );
    $( "#maxValue" ).val( ui.values[ 1 ] );
});
</script>

<form action="">
    <div id="slider-range"></div>
    <label for="amount">Price range:</label><input type="text" id="amount" style="border:0; color:#28b1ff; font-weight:bold;" />
    <input id="minValue" name="minValue" type="hidden" />
    <input id="maxValue" name="maxValue" type="hidden" />
    <input id="randomGiftButton" type="submit" name="submit" value="go" />
</form>

I am using the jQuery UI range slider demo code (http://jqueryui.com/demos/slider/#range) to create a price range slider. I want to transfer the values selected from this range slider to the get params of minValue & maxValue.

I have been trying to do this by getting the JavaScript to update the hidden fields in the form below but this doesn't seem to work as at the moment the page reloads with the URL query string looking like this: ?minValue=&maxValue=&submit=go and no values have been transferred from the range slider. How do I get the values from the range slider to the query string?

Thanks in advance.

<script type="text/javascript" language="javascript">
$(function() {
    $( "#slider-range" ).slider({
        range: true,
        min: 1,
        max: 3000,
        values: [ 1, 300 ],
        slide: function( event, ui ) {
            $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ]);
        }
    });
    $( "#amount" ).val( "£" + $( "#slider-range" ).slider( "values", 0 ) + " - £" + $( "#slider-range" ).slider( "values", 1 ) );
    $( "#minValue" ).val( ui.values[ 0 ] );
    $( "#maxValue" ).val( ui.values[ 1 ] );
});
</script>

<form action="">
    <div id="slider-range"></div>
    <label for="amount">Price range:</label><input type="text" id="amount" style="border:0; color:#28b1ff; font-weight:bold;" />
    <input id="minValue" name="minValue" type="hidden" />
    <input id="maxValue" name="maxValue" type="hidden" />
    <input id="randomGiftButton" type="submit" name="submit" value="go" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下壹個目標 2024-12-25 15:25:43

您正在滑块代码之外设置 min/maxValues。将这些行移至 slide 回调函数中。

slide: function( event, ui ) {
    $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ]);
    $( "#minValue" ).val( ui.values[ 0 ] );
    $( "#maxValue" ).val( ui.values[ 1 ] );
}

You're setting the min/maxValues outside of the slider's code. Move those lines into the slide callback function.

slide: function( event, ui ) {
    $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ]);
    $( "#minValue" ).val( ui.values[ 0 ] );
    $( "#maxValue" ).val( ui.values[ 1 ] );
}
无悔心 2024-12-25 15:25:43

当您在 document.ready 上调用以下两行时,ui 变量不存在:

$( "#minValue" ).val( ui.values[ 0 ] );
$( "#maxValue" ).val( ui.values[ 1 ] );

您是否检查过 ui 变量内部的 >slide 回调函数来确保您正确访问数据?如果您还没有,请尝试执行此操作,看看您的开发人员工具控制台中会出现什么:

    slide: function( event, ui ) {
        console.log(ui);
        $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ]);
    }

此外,文档还说明了您访问这些值的方式:

var values = $("#slider-range").slider( "option", "values" );

它返回一个数组,因此我假设要访问您将执行此操作的值:

var min = values[0],
    max = values[1];

When you call the following two lines on document.ready the ui variable does not exist:

$( "#minValue" ).val( ui.values[ 0 ] );
$( "#maxValue" ).val( ui.values[ 1 ] );

Have you inspected the ui variable inside of the slide callback function to make sure that you are accessing the data correctly? If you have not yet, then try this and see what comes up in your Developer Tools Console:

    slide: function( event, ui ) {
        console.log(ui);
        $( "#amount" ).val( "£" + ui.values[ 0 ] + " - £" + ui.values[ 1 ]);
    }

Also the docs state this is how you access the values:

var values = $("#slider-range").slider( "option", "values" );

Which returns an array so I assume to access the values you would do this:

var min = values[0],
    max = values[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文