0

我的html:

<form action="submitPage.php" method="post" onsubmit="savePathInVar();"> 
  <table>
  <tr>
    <td><img src="<?php echo $path; ?>" onclick="changecolor(this);" /></td>
    <td><img src="<?php echo $path; ?>" onclick="changecolor(this);" /></td>
  </tr>
    <input type="hidden" value="ClickedImaged" />
    <input type="submit" value="Submit" />          
  </table>
</form>

ps:$path = "inventory_images/$id.jpg"; 我的函数changecolor():

function changecolor(img)
{
var images = document.getElementsByTagName("img");
for(var i=0,j=images.length; i<j; i++)
{
    images[i].style.borderColor="#000";
}
img.style.borderColor="#F00";
//Operate on img location as before
savePathInVar(img.src);
}

我的函数 savePathInVar():

function savePathInVar(ImgLocation)
{
//How do i save the path of the clicked image in a variable now?
}

我现在如何将单击图像的路径保存在变量中?以及如何将此变量放入表格下方的输入隐藏字段中?谁能帮助我?
如果有人可以帮助我,那就太好了.. 我对 javascript 很陌生 :) 问候!

4

2 回答 2

0

如果我错了,请纠正我,但是您想将单击图像的路径保存在字符串中,并将此字符串保存在输入的值中?在这种情况下,它非常简单。注意添加的表单名称和输入。

<form name="form_name" action="submitPage.php" method="post" onsubmit="savePathInVar();"> 
  <table>
  <tr>
    <td><img src="<?php echo $path; ?>" onclick="changecolor(this);" /></td>
    <td><img src="<?php echo $path; ?>" onclick="changecolor(this);" /></td>
  </tr>
    <input name="input_name" type="hidden" value="ClickedImaged" />
    <input type="submit" value="Submit" />          
  </table>
</form>

function savePathInVar(ImgLocation)
{
    //if you want it to be a local variable
    var str = ImgLocation;
    //if you want it to be a global variable - to be handled with care*
    str = ImgLocation;
    document.form_name.input_name.value = str; 

}

* 使用全局变量时要小心。您可以在此处找到更多详细信息:Javascript 中变量声明语法的区别(包括全局变量)?

于 2013-05-23T09:01:35.300 回答
0

只需保存 var,然后将其设置为字段。

var imgLoc;

function savePathInVar(ImgLocation)
{
    imgLoc = ImgLocation
    document.getElementById('hiddenImgLoc').value = ImgLocation
}

示例:(我已经删除了该字段上的隐藏属性,以便您可以看到它。) http://jsfiddle.net/E4RG3/

于 2013-05-23T09:07:51.207 回答