Javascript 从$(文档)获取值。准备好了吗
我正在从temp.php读取温度,并用下面的代码将其显示在index.php上。 我的问题是我不能将这些信息用于其他任何事情 如果有任何建议,我将不胜感激Javascript 从$(文档)获取值。准备好了吗,javascript,jquery,Javascript,Jquery,我正在从temp.php读取温度,并用下面的代码将其显示在index.php上。 我的问题是我不能将这些信息用于其他任何事情 如果有任何建议,我将不胜感激 <div id = "temperature"></div> // displays as expected $(document).ready(function(){ setInterval(function(){ $("#temperature").load('temp.php') }, 1000); });
<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"
//按预期显示
$(文档).ready(函数(){
setInterval(函数(){
$(“#温度”).load('temp.php'))
}, 1000);
});
temp1=document.getElementById('temperature').value;//这不管用
如果(temp1==72){做某事}//无法获取“温度”的值
您应该在回调中得到如下响应
<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>
$(“#success”).load(“/not-here.php”),函数(响应、状态、xhr){
如果(状态=“错误”){
var msg=“抱歉,出现错误:”;
$(“#error”).html(msg+xhr.status+“”+xhr.statusText);
}
});
s没有值,但是您是否正在尝试在不等待加载完成的情况下立即运行此程序?我从“temp.php”获得的信息每秒都会更新。我想不断地与“如果”进行比较。我遇到的问题是,在您的案例中获取“温度”或“成功”中的数据,并将其用于。。。如果(success==72){做某事};您得到的响应是json、文本还是xml?如果您想在div中使用resonse,应该像innerHTML not值一样,因为temperature不是form元素。它是从temp.php页面上的一个echo获得响应的,所以我假设它是text。是的,我尝试了x=document.getElementById(“temperature”).innerHTML;