从$(document).ready获得价值

时间:2017-09-17 03:42:24

标签: javascript jquery

我正在从temp.php中读取温度读数并使用下面的代码将其显示在我的index.php上。 我的问题是我不能将这些信息用于其他任何事情 我很感激任何建议。

<div id = "temperature"></div>  // displays as expected

$(document).ready(function(){
setInterval(function(){
$("#temperature").load('temp.php')
}, 1000);
});

temp1 = document.getElementById('temperature').value; // this doesn't work
if (temp1 == 72) {do something}; //cannot get the value of "temperature"

1 个答案:

答案 0 :(得分:-1)

您应该在回调中获得响应,如下所示

<script>
$( "#success" ).load( "/not-here.php", function( response, status, xhr ) {
  if ( status == "error" ) {
    var msg = "Sorry but there was an error: ";
    $( "#error" ).html( msg + xhr.status + " " + xhr.statusText );
  }
});
</script>