当前位置: 代码迷 >> JavaScript >> 从动态javascript onclick函数获取图像路径
  详细解决方案

从动态javascript onclick函数获取图像路径

热度:4   发布时间:2023-06-05 09:44:59.0

我正在显示一个页面,其中包含所有产品详细信息,例如productname,productdetails,product image等。单个产品中包含多种类型。 首先,只应显示一种产品。 根据类型动态创建单选按钮。 单击单选按钮并选择其他类型的图像时,必须更改产品颜色。 我已经传递了$ color值,并且productcolor相应地进行了更改。 1.如何更改图像? 我们如何使用img_id检索img? 2.选择数量后,当我发布cart.php时,数量值在cart.php中返回0。为什么会这样?

        <table width="1000" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="300" height="600" align ="center" ><img id ="img" src="inventory_images/<?php echo $img_id; ?>.jpg" width="300" height="400" alt=""/></td>
            <br>

        <p id = "test"><?php echo $img_id; ?></p>

            <td width="150" height = "400" align="center"  >

             <p align="left"><?php echo $name; ?></p>
             <p align ="left" id = "demo"><?php echo $color; ?></p>
             <p align = "left"><?php echo $product_detail; ?></p>

             <p align = "left">Price : $<?php echo $product_price; ?></p>


              <?php while($rows = mysqli_fetch_array($result))
    {
        $color = $rows["clr"];
        $rimg_id = $rows["img_id"];



          ?>      



<ul align="left" id="menu">
<li><p onclick="myFunction('<?php echo $color; ?>' ,'<?php echo $rimg_id; ?>');"><input   type ="radio" name="radio" id ="php" value="<?php echo $color; ?>" >
<?php echo $color; ?></p>
</li>
</ul>


 <?php } ?>

<script type="text/javascript" >
function myFunction(a,b) {


       document.getElementById("demo").innerHTML = a;
    document.getElementById("test").innerHTML = b;
    document.getElementById("imgid").value = b;
    //document.getElementById("img").src= "inventory_images/.jpg";
       //how to get my images stored in my folder??



}
</script>

 <p align = "left" style="color:#33F">Quantity</p> <input type='button' name='add' onclick='javascript: document.getElementById("qty").value++;' value='+'/>
<input type='text' name='qty' id='qty' value = '1' size = '1' />
</div><input type='button' name='subtract' onclick='javascript: subtractQty();' value='-'/>
<script type="text/javascript">
        function subtractQty(){
            if(document.getElementById("qty").value - 1 < 1)
                return;
            else
                 document.getElementById("qty").value--;
        }
        </script>




              <form id = "add" name = "Add to cart" method = "POST" action = "cart.php" >
             <p><input type = "hidden" id="imgid" name = "imgid" value =""><p>  
              <p align="left"><input type = "submit" name = "submit" id = "submit"  value = "Add to Cart" ><p>
        </form>
  1. 您必须为每种类型创建一个p元素。 在一段时间内做。

     <ul align="left" id="menu"> <?php while($rows = mysqli_fetch_array($result)) { $color = $rows["clr"]; $rimg_id = $rows["img_id"]; ?> <li> <p onclick="myFunction('<?php echo $color; ?>' ,'<?php echo $rimg_id; ?>');"> <input type ="radio" name="radio" value="<?php echo $color; ?>"> <?php echo $color; ?> </p> </li> <?php } ?> </ul> 
  2. 您必须在图像src加载程序中串联函数参数b:

     <script type="text/javascript" > function myFunction(a,b) { document.getElementById("demo").innerHTML = a; document.getElementById("test").innerHTML = b; document.getElementById("imgid").value = b; document.getElementById("img").src= "inventory_images/" + b +".jpg"; //how to get my images stored in my folder?? } </script> 
  相关解决方案