我需要帮助,拜托!我有一个范围输入:
<input type="range" name="set_workers" class="set_workers" id="set_workers" min="0" max="10">
<input type="submit">
我有一个在页面加载时运行的 ajax 脚本来获取一些值。其中一个值应该在最大范围内输出。所以我想要一个具有动态最大值的范围输入。有什么办法吗?我应该在 php 中回显一个范围以将其放在 ajax 响应中吗? 这是我的 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);
}
});
}
还有 php 代码:
$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"
));
我从 php 中删除了一些无用的部分,php 不是问题,正在工作,相同的 json,但我不知道如何将该值放入范围的最大值。 谢谢大家!
最佳答案
是的,您绝对应该通过来自 php 的 echo
检索最大范围值,然后将该值传递给输入的 max 属性。像这样的东西就可以了。
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);
}
});
}
但是- 您需要修改您的响应,以便您可以在 ajax 成功函数中访问它。您访问它的方式将因您格式化 PHP 响应的方式而异。如果您发布 PHP 代码,我们很乐意为您提供帮助。
关于javascript - ajax 响应超过范围的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433829/