2017-09-26 43 views
2

我需要幫助,請!我有一個範圍輸入:ajax響應超出範圍的最大值

<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,但我不知道如何將該值放入範圍的最大值。 謝謝大家!

+0

您能詳細說明「其中一個值應該輸出超過最大範圍」嗎?你在談論從ajax調用返回的數據嗎?你可以改變max的屬性,當ajax調用返回時,它的大小與響應數據相關 –

+0

yes,我的意思是php會回顯一個值,該值應該是範圍爲 –

+0

的max =「..」(屬性)在你的ajax成功函數中,你可以做'$(「#workers」)。attr(「max」,val [1]);'因爲這些數據在POST上返回get_cost_upgrade,所以你不需要echo因爲你在ajax調用上返回這些數據。 –

回答

1

是的,您應該絕對通過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代碼,很樂意提供幫助。

+0

謝謝你的好意,先生!我編輯了我的帖子...忘了關於PHP。最後一行在PHP和Ajax是相對的....我的意思是我不知道該怎麼做... –

+1

@BoteaFlorin,你現在應該可以通過它的關鍵訪問PHP json對象的值。我會更新我的代碼以便爲您反映。 –

+1

@BoteaFlorin請參閱我的更新,並告訴我它是否有幫助。 –