我需要一些建議如何保存或使用我的AJAX輸出在我的if(語句)。這就是代碼的樣子,所以我希望你明白我在尋找什麼。IF(聲明)中使用AJAX值...動畫
setInterval("yourAjaxCall()",1000);
function yourAjaxCall()
{
$.ajax({
url: 'api.php', //the script to call to get data
data: "", //you can insert url argumnets here to pass to api.php
//for example "id=5&parent=6"
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var id = data[0]; //get id
var vname = data[1]; //get name
$("#square").css({"background-color" : vname});
}
});
};
</script>
</head>
<body>
<div id="square" style="width:200px; height:200px; position:relative; ">
</div>
<script>
EDIT $color = output from ajax(background-color of square) //HOW DO I SAVE IT OR USE IT?
/* Start animation */
setInterval("timedani()",10000);
function timedani(){
/* Stop animation when button is clicked */
$("#stop").click(function(){
$("#square").stop();
});
//First TR-bubble
//$("#ball").animate({left: 'x', top: 'y'}, 1500);
if($color == 'pink'){ // PROBABLY WRONG
$("#square").animate({left: 510, top: 75}, 1500);
$("#square").animate({left: 0, top: 75}, 1500);
}
else if($color == 'blue'){ // PROBABLY WRONG
$("#square").animate({left: 0, top: 475}, 1500);
$("#square").animate({left: 0, top: 0}, 1500);
}
};
$("#stop").click(function(){
$("#kvadrat1").stop();
});
$("#back").click(function(){
$("#kvadrat1").animate({left: '0px', top: '0px'}, 1500);
});
</script>
偉大的快速和簡單的答案!順祝商祺
使用==在,如果像如果($顏色= ='粉紅') – Greenhorn
是的,這很簡單,但主要問題是我如何從ajax得到我的輸出在IF語句中。聽說這是不可能保存爲PHP變量,但必須有另一種方式? @Ram – Hilven
這是JavaScript的「雞或蛋」問題。請閱讀此:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call –