duanditang2916 2015-10-09 19:12
浏览 307
已采纳

循环中document.getElementById的结果保持不变

<?php for($i=0; $i< mysqli_num_rows($result); $i++){  ?>

<table id="t01" style="width:100%">
<tr>
<th colspan="2"><?php print_r($results[$i]['arr_question']); ?></th>
  </tr>
  <tr>
    <td><?php print_r($results[$i]['a']); ?></td>
    <td><?php print_r($results[$i]['b']); ?></td>
  </tr>
  <tr>
    <td><?php print_r($results[$i]['c']); ?></td>
    <td><?php print_r($results[$i]['d']); ?></td>
  </tr>
  <tr> 
  <td colspan="2"><button type="button" onClick="document.getElementById('').innerHTML= '<?php print_r($results[$i]['answer']); ?>'"> Answer </button>
  <p id=''></p>
  </td>
  </tr>
  <tr> <td colspan="2"><?php print_r($results[$i]['description']); ?></td>
  </tr>
</table>
</br>
<?php } ?>

In this code document.getElementById('').innerHTML is not working properly, it returns the same value while clicking on button in loop, when i put id equals $i it return the value in same place at every loop, it overrides the data at same place, when i put any static value it returns same. What should i do for getting the different value at different places. Any help will be appreciable.

  • 写回答

1条回答 默认 最新

  • doushi5024 2015-10-09 19:16
    关注

    You cannot have empty ids, assign an id as follows:

     <p id='p-<?php echo $i; ?>'></p>
    

    And use it as follows in the query selector:

    <button type="button" onClick="document.getElementById('p-<?php echo $i; ?>').innerHTML= '<?php print_r($results[$i]['answer']); ?>'"> Answer </button>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据