doushi5752 2012-04-18 09:15
浏览 680

如何使用javascript在html循环中获取动态id的值?

I am creating a drag and drop system using redips javascript.

This is my script using html and php to generate the data

<div id="base">  
   <table>
     <tbody>
       <?php 
          foreach($deviceID as $row)
          { 
             echo '<tr><td><div class="drag">'.$row['description'].'<input type="hidden" id="bus" value="'.$row['description'].'"></div></td></tr>'; 
          }
        ?>
       </tbody>
    </table>
</div>

This is the fragment of my javascript file.

var redips_init;
redips_init = function () {
    // reference to the REDIPS.drag
    var rd = REDIPS.drag;
    // initialization
    rd.init();
        rd.drop_option = 'shift';
    rd.animation_shift = true;

        rd.myhandler_dropped = function () {            
            alert($('#bus').val());
        }
};
  • 写回答

2条回答 默认 最新

  • dongyang7152 2012-04-18 09:30
    关注

    I am not good with php but what i usually do in jsp is create a local index variable and append it to each of the ID attribute in the loop.

    So your code would look something like this

      <?php 
          $index=0;    
          foreach($deviceID as $row)
              { 
                 echo '<tr><td><div class="drag">'.$row['description'].'<input type="hidden" id="bus'.$index++.'" value="'.$row['description'].'"></div></td></tr>'; 
              }
            ?>
    

    In javascript, your dragAndDrop event should return an index location by which u can get appropriate value.

    Now you can use this dynamic ID in your JavaScript to do whatever you want to do.

    评论

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题