大家好,我用PHP动态生成四个FORM,每个FORM都有两个隐藏值,我怎么可以在JS里得到该隐藏值比如PID的值?
<html>
<head>
<style>
img {border:0px;}
</style>
<script type="text/javascript">function addtocart()
{
 var mesg = document.getElementById("msg"); var id = document.addcart.pid.value;
 alert(id);
}
</script> </head>
<body><table>
<tr>
<td><table>
<form name="addcart" method="POST" action="">
<tr>
<td>
<img src="uploads/thumb/1189965375.jpg" />
</td>
<td>New Casing<br/>Price: 300<br/>Promotion Price: 100<br/>
<input type="hidden" name="pid" value="11">
<input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
</td>
</tr>
</form>
</table>
<table>
<form name="addcart" method="POST" action="">
<tr>
<td>
<img src="uploads/thumb/1189965013.jpg" />
</td>
<td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
<input type="hidden" name="pid" value="10">
<input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
</td>
</tr>
</form>
</table>
<table>
<form name="addcart" method="POST" action="">
<tr>
<td>
<img src="uploads/thumb/1189965013.jpg" />
</td>
<td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
<input type="hidden" name="pid" value="10">
<input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
</td>
</tr>
</form>
</table>
<table>
<form name="addcart" method="POST" action="">
<tr>
<td>
<img src="uploads/thumb/1189965013.jpg" />
</td>
<td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
<input type="hidden" name="pid" value="10">
<input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
</td>
</tr>
</form>
</table>
</body>
</html>

解决方案 »

  1.   

    <html>
    <head>
    <style>
    img {border:0px;}
    </style>
    <script type="text/javascript">function addtocart()
    {
     var mesg = document.getElementById("msg"); var id = document.addcart.pid.value;
    alert(id);
    }
    </script></head>
    <body><table>
    <tr>
    <td><table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965375.jpg" />
    </td>
    <td>New Casing<br/>Price: 300<br/>Promotion Price: 100<br/>
    <input type="hidden" name="pid" value="11">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965013.jpg" />
    </td>
    <td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
    <input type="hidden" name="pid" value="10">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965013.jpg" />
    </td>
    <td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
    <input type="hidden" name="pid" value="10">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965013.jpg" />
    </td>
    <td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
    <input type="hidden" name="pid" value="10">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <script type="text/javascript">
     var ccc = document.forms["addcart"].pid.value;
    alert(ccc);
    </script>
    </body>
    </html>
      

  2.   

    怎么不能用? alert(ccc)出来了呀
      

  3.   

    那是你的form的名字取的一样了, 名字要不一样的或用下面的
    <html>
    <head>
    <style>
    img {border:0px;}
    </style>
    <script type="text/javascript">function addtocart()
    {
     var mesg = document.getElementById("msg"); var id = document.addcart.pid.value;
    alert(id);
    }
    </script></head>
    <body><table>
    <tr>
    <td><table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965375.jpg" />
    </td>
    <td>New Casing<br/>Price: 300<br/>Promotion Price: 100<br/>
    <input type="hidden" name="pid" value="11">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965013.jpg" />
    </td>
    <td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
    <input type="hidden" name="pid" value="10">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965013.jpg" />
    </td>
    <td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
    <input type="hidden" name="pid" value="10">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <table>
    <form name="addcart" method="POST" action="">
    <tr>
    <td>
    <img src="uploads/thumb/1189965013.jpg" />
    </td>
    <td>2 u casing<br/>Price: 200<br/>Promotion Price: 30<br/>
    <input type="hidden" name="pid" value="10">
    <input type="image" src="images/order.jpg" name="submit" ONCLICK="addtocart()"/>
    </td>
    </tr>
    </form>
    </table>
    <script type="text/javascript">
     alert(document.forms[0].pid.value);
    alert(document.forms[1].pid.value);
    alert(document.forms[2].pid.value);
    </script>
    </body>
    </ht
      

  4.   

    您好,因为FORM是动态生成,用户可自由生成FORM,比如五个六个,有没有办法用同一个名字,不然程序很难辨别,
      

  5.   

    alert(document.forms[0].pid.value);
    alert(document.forms[1].pid.value);
    alert(document.forms[2].pid.value);
      

  6.   

    mingxuan3000(铭轩) 辛苦了.
    要那么form干嘛? 重新设计