所以我拥有的很简单。我单击主php页面上的一个按钮,它运行两个功能。一个人使用php页面(仅使用html)创建一个颜色框,然后第二个函数使用javascript改变页面的元素。但是,由于某种原因,第二个功能在50%的时间内不起作用!在XAMPP(本地服务器)上进行测试时,它可以正常工作,但远程测试时,第二个功能有时无法运行。
现在,我的代码将更深入(如果需要)。这是我的主页HTML / PHP代码。 onclick运行两个函数(再次,第二个有时不起作用):
<img src="<?php echo visualizeStatus($t11Status, 11) ?>" alt="" width="130" height="105" id="table11img" onclick="showColorBox(); infoBoxPopulate(11)"/>
showColorBox()实际上只是打开一个颜色框:
function showColorBox()
{
$.colorbox({href:"infoBox.php", left: 400, top: 100, opacity: 0.40});
}
infoBox.php是加载到colorbox中的内容,它只是一个html页面,没有特别说明我使用.php的原因。这就是它的样子(仅在这个复制/粘贴上造型不好,无法在不丢失“代码示例”的情况下进行格式化。所有你需要知道的是我给元素id,所以我可以在下一个编辑它们功能:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Table Info</title>
</head>
<body>
<font color="#450505" size =+5"><div id="tableinfo_number">Table # Error</div></font>
<br>
<font size ="+2"><div id="tableinfo_status">Table Status Error</div>
<div id="tableinfo_party">Party: N/A</div>
<div id="tableinfo_orders">Orders: N/A</div></font>
<br>
<br>
<hr>
<center><p><img id= "tableinfo_seatP" src="images/info_seat.png" width="92" height="42" onclick="showSeatBox(window.tableNumWin)"/><img id= "tableinfo_oneUp" src="images/info_increaseOne.png" width="45" height="50" onclick="increasetStatus(window.tableNumWin)" /><img id= "tableinfo_notif" src="images/info_notification.png" width="56" height="53" onclick="makeRequest(window.tableNumWin)"/></p></center>
</body>
</html>
好吧,以便将页面加载到颜色框中。现在,第二个函数,infoBoxPopulate(11),它只是做一个javascript / ajax函数。它编辑了colorbox php页面的两个部分。一个甚至不需要PHP代码来执行此操作,但它们都会在失败时同时失败。
function infoBoxPopulate(tIDin)
{
// This needs to wait until colorbox is loaded, then do this code.
var tID = tIDin;
var tStatus;
window.tableNumWin = tID;
//var oneUpJS = document.getElementById("tableinfo_oneUp");
//oneUpJS.onclick = function() {increasetStatus(tID);}
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp4=new XMLHttpRequest();
}
else
{ // code for IE6, IE5
xmlhttp4=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp4.onreadystatechange=function()
{
document.getElementById("tableinfo_number").innerHTML= "Table: " + tID;
document.getElementById("tableinfo_status").innerHTML= xmlhttp4.responseText;
populateParty(tID);
}
xmlhttp4.open("GET","populateStatus.php?tID="+tID,true);
xmlhttp4.send();
}
再次,populateStatus只执行一个查询,然后返回responseText。所以,我想我只是误解了这段代码是如何运行的。为什么在远程完成时不会更频繁地工作...可能是因为它需要更多时间才能到达SQL服务器?我会假设一旦加载了php页面,我可以使用javascript编辑它的任何元素,但似乎并非如此。请帮忙! :)
答案 0 :(得分:1)
以下是可能发生的事情:
解决方法是使用颜色框提供的钩子,并在颜色盒告诉你准备就绪后启动该功能:
<img src="<?php echo visualizeStatus($t11Status, 11) ?>" alt="" width="130" height="105" id="table11img" onclick="showBoxAndPopulate();"/>
<script type="text/javascript">
function showBoxAndPopulate()
{
$.colorbox({
href:"infoBox.php",
left: 400,
top: 100,
opacity: 0.40,
onComplete: function() { infoBoxPopulate(11); }
});
}
</script>
检查彩盒网站上的回调,找到最适合您的那个。