jQuery 范围滑块 - 获取表单提交时的范围值?
我设置了一个 jQuery UI 范围滑块。当滑块控件移动时,我的代码会将较低范围值插入到隐藏输入中,并将较高范围值插入到隐藏输入中。这些值随着滑块的移动而不断变化,但有一个问题。例如,如果较低值停在 £60,000,则插入隐藏 div 的值将为 £40,000。它总是有 1 个位置不同步。这是我需要解决的第一个问题。在我的代码中,这些值设置为#minPrice 和#maxPrice。
第二个问题发生在表单提交上。我需要获取隐藏输入的值,并(在表单提交后的页面加载上)将它们传递回显示滑块范围的文本输入,例如 40,000 到 60,000。此输入在代码中的 ID 为#amount。
这是我的代码:
// 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)]);
任何帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个问题,您使用不同的方法来设置 #minPrice/#maxPrice 和 #amount 输入。我清理了代码,使其更具可读性,并将 DOM 对象和其他重要数据存储到变量中。这提高了性能并使代码更容易调试。此重构代码始终报告滑块的正确值。
至于第二个问题,在没有看到其余代码的情况下很难准确地说出应该如何完成该任务,但一般来说,您所要做的就是确保在页面打开时将 #maxPrice 和 #minPrice 隐藏变量设置为正确的值加载并且滑块在实例化时会自动显示正确的信息,因为它在构造时会检查这些值。例如,如果您使用 PHP 并通过 POST 提交表单,那么您可以对这些隐藏变量使用以下代码:
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.
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: