ajax响应超出范围的最大值
I need help, please! I have a range input:
<input type="range" name="set_workers" class="set_workers" id="set_workers" min="0" max="10">
<input type="submit">
I have an ajax script run on page load getting some values. One of that values should be outputted over max range. So I want a range input with dynamic max value. Is there any way to do that? Should I echo a range in php to put it on ajax response? here is my ajax:
window.onload = function() {
var current_building = $('#building_name').val();
$.ajax(
{type: 'post',
url: '../account_handle/get_cost_upgrade.php',
data: {
building: current_building,
},
success: function (val){
$('#display_cost').html(val.test1);
$("#set_workers").attr("max", val.test2);
}
});
}
And the php code:
$sql = "SELECT * FROM $buildcost WHERE lvl = '$lvl'";
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$max_w = $row['max_w'];
echo json_encode(array(
"test1" => "doesn t matter",
"test2" =>
"$max_w"
));
I cutted some useless parts from php, php is not a problem, is working, same json, but i don t know how to put that value into max of range. Thanks all!
Yes, you should absolutely retrieve that maximum range value via an echo
from php, and then pass that value into the the input's max attribute. Something like this will do the trick.
window.onload = function() {
var current_building = $('#building_name').val();
$.ajax(
{type: 'post',
url: '../account_handle/get_cost_upgrade.php',
data: {
building: current_building,
},
success: function (response){
$('#display_cost').html(response);
//This line will set the "max" attribute to whatever you respond with in your PHP,
//although please note how you access that data depends on how you
//format it in your PHP
$("#workers").attr("max", response.max)
//Access the JSON object's value via it's key, like below
console.log(response.test1);
//or
console.log(response.test2);
}
});
}
However- You will need to modify your response in such a way to where you can access it in your ajax success function. How you access it will vary on how you format the PHP response. Happy to help with that if you post your PHP code.