drrkgbm6851 2015-01-17 11:15
浏览 44
已采纳

将元素的DOM位置存储在变量中?

I want the variable $slideNumber to get the number of the <input> element depending on what position it is in the DOM. So if it's the first child, it would get 1, the second child would get 2, etc.

The output would look something like this:

<div>
    <input type="radio" name="slider" id="slide1">
    <input type="radio" name="slider" id="slide2">
    <input type="radio" name="slider" id="slide3">
    <input type="radio" name="slider" id="slide4">
    ...
</div>

Here is the code:

<?php if( have_rows('slides') ):
    while ( have_rows('slides') ) : the_row();

    $slideNumber = // not sure what to do here

    ?>

<input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">

<?php endwhile;endif; ?>
  • 写回答

2条回答 默认 最新

  • douyin6188 2015-01-17 11:26
    关注

    Simply initialise your variable before the loop and increment it on every iteration.

    <?php 
    if( have_rows('slides') ):
        $slideNumber = 1;
        while ( have_rows('slides') ) : the_row();
    ?>
    
    <input type="radio" name="slider" id="slide<?php echo $slideNumber++; ?>">
    
    <?php endwhile;endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效