donglv1831 2017-09-26 18:54
浏览 115
已采纳

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!

  • 写回答

1条回答 默认 最新

  • douxiajia6104 2017-09-26 19:11
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置