【问题标题】:Problems with javascript/php image positioning systemjavascript/php图像定位系统的问题
【发布时间】:2010-01-23 14:44:27
【问题描述】:

我正在做一个涉及在虚拟客厅中移动产品的项目,我有以下功能

<a href="javascript:void(0)" onclick="sendxandy( <? echo $_SESSION['numberOfProducts']; ?> )">Save Positions of Products</a>

然后函数如下:

` 函数 sendxandy(productAmount) { 如果(产品数量 == 1) { location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y; } 如果(产品数量 == 2) { location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y; } 如果(产品数量 == 3) { location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y + "&xthree= " + dd.elements.image3.x + "&ythree=" + dd.elements.image3.y; } 如果(产品数量 == 4) { location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y + "&xthree= " + dd.elements.image3.x + "&ythree=" + dd.elements.image3.y + "&xfour= " + dd.elements.image4.x + "&yfour=" + dd.elements.image4.y; } 如果(产品数量 == 5) { location.href="homeview.php?x=" + dd.elements.image1.x + "&y=" + dd.elements.image1.y + "&xtwo= " + dd.elements.image2.x + "&ytwo=" + dd.elements.image2.y + "&xthree= " + dd.elements.image3.x + "&ythree=" + dd.elements.image3.y + "&xfour= " + dd.elements.image4.x + "&yfour=" + dd.elements.image4.y + "&xfive= " + dd.elements.image5.x + "&yfive=" + dd.elements.image5.y; } `

这个函数就这样继续到图像 10。所以你可以看到图像的坐标保存在 URL 中,以便我可以在 php 中访问它们,我的下一个函数是这个

&lt;a class="code" href="javascript:void(0);" onclick="moveProduct(&lt;? echo $_SESSION['numberOfProducts']; ?&gt;)"&gt;Move Images Back&lt;/a&gt;

在里面我有这个(moveTo 变量是 php 变量,由于某种原因,这个显示器无法打印代码,moveTo 是另一个由第二个脚本提供的函数):

`功能moveProduct(moveAmount) { 如果(移动量 == 1) { 如果(window.dd && dd.elements) { dd.elements.image1.moveTo(, ); } } 如果(移动量 == 2) { 如果(window.dd && dd.elements) { dd.elements.image1.moveTo(, ); dd.elements.image2.moveTo(, ); } } 如果(移动量 == 3) { 如果(window.dd && dd.elements) { dd.elements.image1.moveTo(, ); dd.elements.image2.moveTo(, ); dd.elements.image3.moveTo(, ); } }

` 现在我知道我的循环结构很糟糕:) 但请多多包涵。发生的事情是在 moveProduct 函数内部,无论最后一个 if "moveAmount == " 是什么,那么图像只会被重新定位在那个数字上。例如,如果我设置了上述功能,则只会记住三张图像,不是一两张,也不是四五张,而是三张。我实际上有十个项目,所以我为 10 个项目设置了上述功能,并且只会记住 10 个图像。当我在页面上没有 10 个项目时运行函数 moveProduct 时,什么都没有发生,就像我将加载一张图像,移动它,单击保存,一切看起来都很好,但是当我将它移回记住的坐标时,什么也没有发生。

请帮助任何建议将不胜感激

【问题讨论】:

  • 您的 PHP 生成什么 HTML/JavaScript,您在客户端遇到什么错误?
  • 好的,我用会话变量替换了所有变量,现在如果浏览器中的 if 语句读取两个图像,它可以很好地读取 moveTo 变量: if (moveAmount == 4) { if(window. dd && dd.elements) { dd.elements.image1.moveTo(8, 8); dd.elements.image2.moveTo(8, 53); dd.elements.image3.moveTo(, ); dd.elements.image4.moveTo(, );但是由于项目的数量小于 4,所以这个代码永远不会正确执行?
  • 我用调试器运行了firebug,它说当我尝试点击链接时它实际上并没有移动任何东西它说“moveProduct is not defined”但是当有十个产品时没有问题运行代码
  • 哇,这段代码复制粘贴很开心。查看 for 循环。 :)
  • 是的,我知道 :) 但理论上它应该可以正常工作吗?我的意思是什么会导致 javascript 函数突然“未定义”?

标签: php javascript image function positioning


【解决方案1】:

嘿,我想通了,php 变量返回空白值,这使得 js 函数完全失败:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 2014-01-16
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多