我試圖在我的滑塊中動態設置最大值,最大值是從輸入#max獲得的,我試圖用$(「#max」).val來放這段代碼最大變量,但它不工作。動態設置最大值到滑塊
編輯: 當用戶選擇一個類別,我顯示所有的產品從該類別,並且與AJAX部分I的值設定爲輸入#max(最大價)
//When user select one category, I display all the product from this category, and in my ajax part I
$('#category').change(function() {
$.ajax({
url: 'ajax.php',
data: {category: category},
dataType: 'json',
cache: false,
type: "GET",
success: function(data){
if (data.length > 0) {
data.forEach(function (elem) {
$('#max').val(elem.MaxPrice); //set the Maximum price in the hidden input
$('#listing_annonce_int').append('<div id="title">'+ elem.title +'</div><div id="price">'+ elem.price +'</div>');
})
}
}
})
});
/**************************************/
function showProducts(minPrice, maxPrice) {
$("#listing_annonce_int .listing_annonce_view").hide().filter(function() {
var price = parseInt($(this).data("price"), 10);
return price >= minPrice && price <= maxPrice;
}).show();
}
$(function() {
var options = {
range: true,
min: 0,
max: 20000,
values: [0, 5000],
slide: function(event, ui) {
var min = ui.values[0],
max = ui.values[1];
$("#amount").val(min + " € - " + max + " €");
showProducts(min, max);
}
}, min, max;
$("#slider-range").slider(options);
min = $("#slider-range").slider("values", 0);
max = $("#slider-range").slider("values", 1);
$("#amount").val(min + " €- " + max + " €");
showProducts(min, max);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<select id="category" name="category">
<option value='1'>cat 1</option>
<option value='2'>cat 2</option>
</select>
<input type="text" id="amount"/>
<input type="text" id="max"/>
<div id="slider-range"></div>
謝謝,它不適合我,因爲輸入不是由用戶填寫,當輸入選擇類別更改時,此輸入將獲得他的值,所以我嘗試 $('#category')。change(function(){ $(「#slider-range」).slider(「option」,「max」,parseInt($(#max).val())); }); 但它不工作,有什麼想法? – Eagle
我沒有看到這樣的元素。請提供更多#category的代碼。 –
對不起,我添加了代碼:) – Eagle